我仍然對iOS開發不熟悉,但遇到了一個我無法解決的問題,而且我嘗試在網上查找,但找不到任何東西。實例變量在UINavigationController中的ViewController中不起作用
我正在使用UIImagePickerController來選擇和圖像,我在App Delegate中使用它。當圖像返回時,在imagePickerController:didFinishPickingMediaWithInfo方法中,我想用視圖控制器製作一個新的導航控制器,並將其放在「應用程序」上。
這裏是我如何做它:
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時未加載視圖。 viewDidLoad被稱爲後綴。有沒有辦法延遲方法調用?
爲什麼您需要製作新的導航控制器?只是推動當前的新觀點。 – msgambel
這是一個選項卡式應用程序,我想要複製Instagram,如果您按中間選項卡,您可以拍攝照片,然後再轉到另一個視圖,您無法看到選項卡欄。之後我需要另一個視圖,所以我認爲我應該使用導航控制器。 – Yuriy
另外,如果我在初始photoViewController上推另一個視圖控制器,那裏沒有實例變量的問題。 – Yuriy