UIViewController在被填充后维持状态

时间:2012-08-31 15:06:59

标签: objective-c cocoa uiviewcontroller automatic-ref-counting

在我的应用程序中,我创建了一个BookViewController类,用于显示和动画书籍的页面以及显示用户可以阅读的一组书籍的MainMenuViewController类。

在后一类中,当用户点击其中一本书时,会调用一个函数来创建一个全新的BookViewController实例,但由于某种原因,实例会保持其状态(即它从用户离开的页面)。

如果我把它设为零,怎么会这样?我在这里错过了什么? (注意我正在使用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>

3 个答案:

答案 0 :(得分:1)

您的变量在类的@implementation之外声明(您声明全局变量)。

答案 1 :(得分:0)

我怀疑你使用的是ivars而不是属性。请将bookViewController替换为self.bookViewController

尝试:

if(self){
        self.bookTitle

答案 2 :(得分:0)

因此声明了在新实例中维护其状态的变量:

#import "BookViewController.h"

int currentPage = 0;

@implementation BookViewController

-(id)initWithBookTitle:(NSString *)theBookTitle{
...

所以我设法通过初始化init方法中的变量来解决问题:

-(id)initWithBookTitle:(NSString *)theBookTitle{
    self = [super init];
    if(self){
        currentPage = 0; //added this line
        bookTitle = [NSString stringWithFormat:@"%@", theBookTitle];
        [self setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];

         NSLog(@"init a BookViewController with bookTitle: %@", bookTitle);
    }
    return self;
}

但这并没有解决潜在的问题,即这样做:

bookViewController = [[BookViewController alloc] initWithBookTitle:bookTitle];
[self presentViewController:bookViewController animated:YES completion:nil];

还提供了一个bookViewController,其中包含currentPage的旧值!这可能是因为我没有将它声明为属性,也没有用init方法初始化它......有什么想法吗?