在我的应用程序中,我创建了一个BookViewController
显示书籍页面并为其设置动画的MainMenuViewController
类,以及一个显示用户可以阅读的一组书籍的类。
在后一类中,当用户点击其中一本书时,会调用一个函数,该函数应该创建一个全新的 实例BookViewController
,但由于某种原因,该实例保持其状态(即它从用户离开的页面恢复)。
如果我将它设置为 nil,怎么会这样?我在这里想念什么?(请注意,我使用的是 ARC)。
MainMenuViewController.m
@interface MainMenuViewController ()
@property (strong) BookViewController *bookViewController;
@end
@implementation MainMenuViewController
@synthesize bookViewController;
-(void)bookTapped:(UIButton *)sender{
NSString *bookTitle;
if(sender == book1button) bookTitle = @"book1";
else if(sender == book2button) bookTitle = @"book2";
bookViewController = nil;
bookViewController = [[BookViewController alloc] initWithBookTitle:bookTitle];
[self presentViewController:bookViewController animated:YES completion:nil];
}
BookViewController.h
@interface BookViewController : UIViewController
-(id)initWithBookTitle:(NSString *)bookTitle;
@end
BookViewController.m
@implementation BookViewController
-(id)initWithBookTitle:(NSString *)theBookTitle{
self = [super init];
if(self){
bookTitle = [NSString stringWithFormat:@"%@", theBookTitle];
[self setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
NSLog(@"init a BookViewController with bookTitle: %@", bookTitle);
}
return self;
}
编辑1:
每次点击一本书时,bookTapped:
都会调用它,并且控制台总是会打印:
2012-08-31 16:29:51.750 AppName[25713:c07] init a BookViewController with bookTitle: book1
因此,如果正在创建一个新实例,BookViewController
它怎么会返回旧实例呢?
编辑2:
我 NSLog(@"bookViewController %@",bookViewController);
在该行之前插入[self presentViewController:bookViewController
。控制台输出为:
2012-08-31 16:37:41.426 Henry[25784:c07] bookViewController <BookViewController: 0x6a21540>
2012-08-31 16:38:23.321 Henry[25784:c07] bookViewController <BookViewController: 0xe425540>
2012-08-31 16:38:53.393 Henry[25784:c07] bookViewController <BookViewController: 0x6839330>