objective-c - 实例变量在 UINavigationController 的 ViewController 中不起作用

标签 objective-c ios

我对 iOS 开发还是个新手,但遇到了一个无法解决的问题,我尝试在网上查找,但还没有找到任何东西。

我正在使用 UIImagePickerController 来选取和图像,并在应用程序委托(delegate)中使用它。当返回图像时,在 imagePickerController:didFinishPickingMediaWithInfo 方法中,我想用 View Controller 创建一个新的导航 Controller 并将其放在“app”上。

这是我的做法:

CustomNavigationController *customNavigationController = [[CustomNavigationController alloc] init];
PhotoViewController *photoViewController = [[PhotoViewController alloc] initWithNibName:@"PhotoViewController" bundle:[NSBundle mainBundle]];

[customNavigationController pushViewController:photoViewController animated:NO];
[customNavigationController.view setFrame:[[UIScreen mainScreen] applicationFrame]];

[photoViewController release];
[self.window addSubview:customNavigationController.view];

//Call method of photoViewController.
[[customNavigationController.viewControllers objectAtIndex:0] addPhotoFromData:info];

[self.tabBarController dismissViewControllerAnimated:YES completion:nil]; //Get rid of UIImagePickerController

但是在 photoViewController 中,我无权访问在 viewDidLoad 中合成和加载的任何实例变量。它们都返回 null。还有一个 tableView,重新加载 tableView 的调用实际上不会导致 tableView 响应。

这是来自 photoViewController 的一些代码

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.photoCount = 0;
    self.timestamp = [[NSDate date] timeIntervalSince1970];
    self.photos = [[NSMutableArray alloc] initWithCapacity:5];
    self.photosData = [[NSMutableArray alloc] initWithCapacity:5];
    self.uploadingCount = 0;

    UINib *customCell = [UINib nibWithNibName:@"CustomTableCell" bundle:[NSBundle mainBundle]];

    [self.tableView registerNib:customCell forCellReuseIdentifier:@"customCell"];
    self.tableView.separatorColor = [UIColor clearColor];

    NSLog(@"%@", self);
}

还有 addPhotoFromData 方法:

- (void)addPhotoFromData:(NSDictionary *)info
{    
   [self.photos addObject:info];
   NSLog(@"%@", self.photos); //Doesn't add "info" and does not return anything when later called from other methods.

   [self.tableView reloadData]; //Doesn't work

在添加 UINavigationController 之前一切正常。我完全迷失了。

编辑:经过多次调试尝试,我发现调用 addPhotoFromData 时未加载 View 。 viewDidLoad被称为后话。有没有办法延迟方法调用?

最佳答案

有什么原因不能让 PhotoViewController 在 viewDidLoad 方法中调用 addPhotoFromData: 吗?您始终可以从 View Controller 访问应用程序委托(delegate)的属性:

YourAppDelegateType * delegate = [UIApplication sharedApplication].delegate;
[self addPhotoFromData:delegate.info];

关于objective-c - 实例变量在 UINavigationController 的 ViewController 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13461314/

相关文章:

ios - GMSMarker 不显示 GroundAnchor iOS

ios - TableView 单元格对齐导致顶部有一些空间

iphone - NSLocalizedString 管理应用程序版本的翻译

php - 将 php 代码转换为 Objective-C

ios - AFNetworking 在 ViewController 之间共享 JSON 数据?

ios - Realm 通知对于 UI 更新触发太快(没有细粒度通知)

ios - 将HTML解析为NSDictionary

iphone - iOS 上的用户体验流程

iphone - 如何打印证书的公钥(Objective-C)

ios - didSelectRowAtIndexPath 时关闭 View 并设置数据