0

在我的应用程序中,我创建了一个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>
4

3 回答 3

1

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

于 2013-01-28T20:31:29.427 回答
0

我怀疑您使用的是 ivars 而不是属性。请替换bookViewControllerself.bookViewController.

尝试:

if(self){
        self.bookTitle
于 2012-08-31T15:22:29.513 回答
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方法中对其进行初始化......有什么想法吗?

于 2012-09-03T14:17:08.127 回答