将值从一个viewcontroller传递到另一个viewcontroller的问题

时间:2012-08-28 10:00:18

标签: iphone ipad

我有一个视图控制器。我在按钮操作上添加了popoverviewcontroller并加载了另一个具有uitableview控制器的viewcontroller。当我在tableview控制器中选择一个值时,我无法将所选值传递给firstviewcontroller。

该值为空

第二个ViewController有UITableview

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [[NSNotificationCenter defaultCenter] postNotificationName:@"RemovePopOverController" object:self];
    selectedValue =   [titleArray objectAtIndex:indexPath.row];

}

firstviewController

-(void)removePopOver:(id)sender{

    [self->popoverController dismissPopoverAnimated:YES];

    NSString *value = m_titleViewController.selectedValue; 


  NSLog(@" Value is %@", value);
}

我不想使用NSUserDefault。请告诉我这个问题的解决方案!

提前致谢。

3 个答案:

答案 0 :(得分:0)

您在设置值之前发布通知。尝试这样做:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    selectedValue =   [titleArray objectAtIndex:indexPath.row];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"RemovePopOverController" object:self];
}

答案 1 :(得分:0)

使用委托作为您的要求,因为最好在这种情况下使用,而不是发布通知。

参考simple-delegate-tutorial-for-ios-development链接。希望对你有所帮助。

答案 2 :(得分:-1)

然后你可以使用像This:

这样的另一种方法

在Appdelegate中创建一个String(例如:stringValue)

首先将您的价值保存到您的Appdelegate中:

 Appdelegate *appdell=[[UIApplication sharedApplication]delegate];
    appdell.stringValue=@"Your Value Goes Here";

当你想要的时候你可以得到这个价值。

Appdelegate *appdell=[[UIApplication sharedApplication]delegate];

requiredvalue=appdell.stringValue;
相关问题