在我的应用程序中,我有两种不同的方式来访问相同的UIViewController,在途中是通过使用故事板创建的2个父ViewControllers,这种方式完美地传递和id通过每个segue过渡。
另一种方式(这是我需要帮助的)直接来自RootViewController上按下的按钮,它也只是将id传递给一个名为Event的自定义对象。每个转换的代码如下:
来自segue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
Event *event = [nEventList objectAtIndex:path.row];
EventInfo *eventInfo = segue.destinationViewController; //Create new ViewController
eventInfo.eventID = event.id;
}
从按钮
- (void)FeaturedPressedAction:(id)sender
{
UIButton *button = (UIButton *)sender;
int tag = button.tag;
EventInfo *eventInfo = [[EventInfo alloc] init]; //Create new ViewController
eventInfo.eventID = @"1"; //There is an event with id '1'
[self.navigationController pushViewController:eventInfo animated:YES];
}
设置ViewController的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIImage *image = [UIImage imageNamed:@"title.png"];
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:image];
SingletonClass* myapp = [SingletonClass sharedInstance];
events = [myapp getEvents];
// Loop to filter out the events that Do not have this ID
Event *event;
BOOL found = false;
for (int i = 0; i < [events count]; i++) {
if(!found){
event = [events objectAtIndex:i];
if([event.id isEqualToString: eventID]){
found = true;
NSLog(@"Found the event with title %@", event.title);
}
}
}
self.eventTitle.text = event.title;
self.eventLocation.text = event.location;
self.eventDate.text = event.date;
self.eventTime.text = @"22:00";
self.eventDescription.text = event.details;
}
它发现事件正常,因为我可以看到NSLog左边的那个,当按下按钮时没有任何视图加载。我哪里错了?
答案 0 :(得分:2)
您应该从故事板中实例化它,而不是分配您的EventInfo*
。
所以在FeaturedPressedAction
中用以下内容替换初始化行:
EventInfo *eventInfo = [self.storyboard instantiateViewControllerWithIdentifier:@"..."];
确保使用相同的标识符识别Interface Builder中的ViewController。用有意义的东西替换......