简体   繁体   English

将值从一个视图控制器传递给另一个视图控制器的问题

[英]Issue in passing value from one viewcontroller to another viewcontroller

I have one view controller. 我有一个视图控制器。 I added popoverviewcontroller on button action and loaded another viewcontroller which has a uitableview controller. 我在按钮操作上添加了popoverviewcontroller,并加载了另一个具有uitableview控制器的viewcontroller。 When I select a value inside the tableview controller, I am not able to pass the selected value into firstviewcontroller. 当我在tableview控制器中选择一个值时,无法将所选值传递给firstviewcontroller。

The value is coming as null 该值将为空

Second ViewController is having UITableview 第二个ViewController具有UITableview

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

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

}

firstviewController firstviewController

-(void)removePopOver:(id)sender{

    [self->popoverController dismissPopoverAnimated:YES];

    NSString *value = m_titleViewController.selectedValue; 


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

I don't want to use NSUserDefault. 我不想使用NSUserDefault。 Please advice me with a solution for this issue! 请为我提供解决此问题的方法!

Thanks in advance. 提前致谢。

You are posting the notification before setting the value. 您要在设置值之前发布通知。 Try doing it like this: 尝试这样做:

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

Use delegate for your requirement as it best pratcice to use in this situation rather than posting notification. 使用委派来满足您的要求,因为在这种情况下最好使用它,而不要发布通知。

Refer simple-delegate-tutorial-for-ios-development link. 请参阅simple-delegate-tutorial-for-ios-development链接。 Hope helpful. 希望对您有所帮助。

Then you can use Another method like This : 然后,您可以使用其他方法,例如:

Create a String in Appdelegate (ex: stringValue) 在Appdelegate中创建一个字符串(例如:stringValue)

First save your Value into your Appdelegate: 首先将您的价值保存到您的Appdelegate中:

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

when ever you want you can get this value. 每当您想要时,您都可以获取此值。

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

requiredvalue=appdell.stringValue;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数组从一个viewcontroller传递到另一个 - passing array from one viewcontroller to another 使参数从一个ViewController传递到另一个的崩溃 - crash passing parameter from one viewcontroller to another 通过UINavigationController将整个UIView从一个ViewController传递到另一个 - Passing an entire UIView From One ViewController to another via UINavigationController 使用segue将Int从一个ViewController传递到另一个 - passing an Int from one ViewController to another using a segue 将核心数据属性从viewController传递到另一个viewController - Passing Core Data attributes from viewController to another viewController 将数据传递给ViewController问题 - Passing data to the ViewController issue 如何从一个ViewController在另一个View Controller中为NSString设置值 - How to set value for NSString in another view controller from one viewcontroller 将IBaction传递给另一个ViewController - Passing an IBaction to another ViewController 从一个viewcontroller选择到另一个viewController的多个数组元素 - Multiple array elements selected from one viewcontroller to another viewController 从另一个viewController显示一个viewController,当它们都是UITabBarController的一部分时 - showing one viewController from another viewController, when both are part of a UITabBarController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM