我實際上試圖弄清楚Objective-C中的繼承是如何工作的。我的問題是,我的obj。總是返回「null」。iOS - 繼承如何工作?
這是我的代碼:
編輯:添加了代碼的其餘部分。
// ReportViewController.h
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import "IAPHelper.h"
@class Report, Category, GADBannerView;
@interface ReportViewController : UIViewController <UIWebViewDelegate,
NSFetchedResultsControllerDelegate> {NSString* _werbung;}
@property (nonatomic, strong) GADBannerView *bannerView;
@property (nonatomic, retain) NSString* werbung;
- (id)initWithReport:(Report *)report category:(Category *)category ;
@end
// ReportViewController.m
#import "ReportViewController.h"
#import "IAPHelper.h"
@interface ReportViewController()
- (void)loadReport;
- (void)setupFetchRequest;
- (void)resizeNavigationContentViewToHeight:(CGFloat)height;
- (NSString*) werbung;
- (void)setWerbung:(NSString *)newwerbung;
@end
@implementation ReportViewController
@synthesize werbung = _werbung;
-(NSString*) werbung {
return _werbung;
}
- (void)setWerbung:(NSString *)newwerbung {
_werbung= newwerbung;
}
//Werbung ausblenden
NSLog(@"Check for bought products");
if ([_werbung isEqual: @"gekauft"]) {
self.bannerView.hidden = TRUE;
}
- (void)viewDidLoad
{
[super viewDidLoad];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
{
self.edgesForExtendedLayout=UIRectEdgeNone;
self.navigationController.navigationBar.translucent = NO;
}
//ADMob
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) {
_bannerView = [[GADBannerView alloc] initWithFrame: CGRectMake(20.0,850.0,728,90)];}
if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight) {
_bannerView = [[GADBannerView alloc] initWithFrame: CGRectMake(-10,615.0,728,90)];}
if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft) {
_bannerView = [[GADBannerView alloc] initWithFrame: CGRectMake(-10,615.0,728,90)];}
}
else
_bannerView = [[GADBannerView alloc] initWithFrame: CGRectMake(0,410,320,50)];
//initWithAdSize:kGADAdSizeBanner];
//initwithframe:CGRectMake(0.0,0.0,320,50)];
self.bannerView.adUnitID = @„xxxxxxxxxxxxxxxxx「;
self.bannerView.rootViewController = self;
GADRequest *request = [GADRequest request];
// Enable test ads on simulators.
[self.view addSubview:(_bannerView)];
request.testDevices = @[ GAD_SIMULATOR_ID, @„xxxxxxxxxxxxxxxxxxxxxxx「 ];
[self.bannerView loadRequest:request];
//Werbung ausblenden
NSLog(@"Check for bought products");
if ([_werbung isEqual: @"gekauft"]) {
self.bannerView.hidden = TRUE;
}
NSLog(@"%@",_werbung);
NSLog(@"%@",self.werbung);
}
// IAPHelper.m
#import "IAPHelper.h"
#import <StoreKit/StoreKit.h>
#import "ReportViewController.h"
@interface IAPHelper() <SKProductsRequestDelegate, SKPaymentTransactionObserver>
@end
@implementation IAPHelper
- (id)initWithProductIdentifiers:(NSSet *)productIdentifiers
{
//self = [super init];
if ((self = [super init])) {
// Store product identifiers
_productIdentifiers = productIdentifiers;
// Check for previously purchased products
_purchasedProductIdentifiers = [NSMutableSet set];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
for (NSString * productIdentifier in _productIdentifiers) {
BOOL productPurchased = [[NSUserDefaults standardUserDefaults] boolForKey:productIdentifier];
if (productPurchased) {
[_purchasedProductIdentifiers addObject:productIdentifier];
NSLog(@"Previously purchased: %@", productIdentifier);
if ([productIdentifier isEqual:@"XXXXXXXXXXXXXXXXXXXXXXXXXX"]) {
ReportViewController *rvc = [[ReportViewController alloc] init];
rvc.werbung = @"gekauft";
NSLog(@"werbung gekauft!");
NSLog(@"%@", rvc.werbung); <- log's @"gekauft";
} else {
NSLog(@"Not purchased: %@", productIdentifier);
}
}
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
}}
return self;
}
我的問題是:我做錯了什麼?也許你對我也有很好的教程?
編輯:你是對的,它不是關於繼承。我的解決方案與UserDefaults一起工作。
你能告訴,哪些對象變爲空嗎? – Hemang
NSLog(@「%@」,_ werbung); < - 這是「null」 NSLog(@「%@」,self.werbung); < - 這也是「空」 //也在代碼btw中標記:) –
那麼,您將代碼行設置爲不是零的東西? – gnasher729