我实际上试图弄清楚 Objective-C 中的继承是如何工作的。我的问题是,我的对象。总是返回“空”。
这是我的代码:
编辑:添加了其余代码。
// 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。
最佳答案
这不是继承的问题——继承控制子类将从其父类获得的行为。这个问题似乎是其中一个实例。
ReportViewController
是一个类。所以这不是一个真正的 Actor 。它只是对所创建的任何 ReportViewController
将如何运行的描述。就像一部宪法。
当您调用 alloc] init]
时,您创建了一个新的 View Controller 实例。然后您将广告设置为在该实例上购买。您不会将实例放在任何地方或以其他方式保留它。因此,该实例不复存在。
在其他地方,在一个完全不同的实例中,您检查了广告值(value)。没有人告诉那个实例任何事情。所以您会看到 nil
值。
认为它与 NSString
完全一样。在下面的代码中,stringB
应该改变值吗?
NSMutableString *stringA = [[NSMutableString alloc] init];
NSMutableString *stringB = [[NSMutableString alloc] init];
[stringA appendString:@"Mo' string for ya'"];
关于iOS - 继承是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27686903/