如何在popViewControllerAnimated中的视图之间传递值?

时间:2022-03-04 18:30:40

I want to know how to pass values between views in popViewControllerAnimated .

我想知道如何在popViewControllerAnimated中的视图之间传递值。

Here is my scenario: I have a view which contains tableview on selecting the cell we go to another view where i need to enter value in textbox and i click a button to go back to the previous view where i need to display the textbox value in the table view cell.

这是我的场景:我有一个视图,其中包含有关选择单元格的表视图,我们转到另一个视图,我需要在文本框中输入值,然后单击按钮返回上一个视图,我需要在其中显示文本框值表格视图单元格。

How can i do this ?

我怎样才能做到这一点 ?

This what i have done:

这就是我所做的:

NewContact *nc = [[NewContact alloc] initWithNibName:@"NewContact" bundle:nil];
// ...
// Pass the selected object to the new view controller.
nc.name=[firstName text];
//[self.navigationController pushViewController:nc animated:YES];
[self.navigationController popViewControllerAnimated:YES];
[nc release];  

4 个解决方案

#1


4  

In ViewControllerB, declare delegate and set action for popViewControllerAnimated:

在ViewControllerB中,为popViewControllerAnimated声明委托和设置动作:

@interface ViewControllerB : UIViewController {
    id delegate;
}

@property (nonatomic, retain) id delegate;

@synthesize delegate;

- (id) init ... {
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                              style:UIBarButtonItemStyleBordered
                                                                             target:self
                                                                             action:@selector(didBack:)] autorelease];
}

- (void) didBack:(id)sender {
    if ([delegate respondsToSelector:@selector(setProperty:)]) { 
        [delegate setProperty:property];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

In ViewControllerA, provide a function to set the local property and set delegate:

在ViewControllerA中,提供一个函数来设置本地属性并设置委托:

ViewControllerB controllerB = [[ViewControllerB alloc] init...];
[controllerB setDelegate:self];
[self.navigationController pushViewController:controllerB animated:YES];
[controllerB release];

#2


1  

You need to store value in one of the global variable for example you can declare in appDelegate file. See this post for that

您需要将值存储在一个全局变量中,例如您可以在appDelegate文件中声明。看到这篇文章

If you are using UITextField then you can store value in above variable from UITextField in below delegate method.

如果您正在使用UITextField,那么您可以在UITextField中将值存储在以下委托方法中。

- (BOOL)textFieldShouldReturn:(UITextField *)textField;

Hope this help.

希望这有帮助。

#3


1  

You could use NSNotificationCenter for this, passing an object along with the call.

您可以使用NSNotificationCenter,将对象与调用一起传递。

Howto: Send and receive messages through NSNotificationCenter in Objective-C?

Howto:通过Objective-C中的NSNotificationCenter发送和接收消息?

#4


0  

Based on your requirement in your comment,

根据您在评论中的要求,

Say you navigate from ViewControllerA instance to ViewControllerB instance and you wish to call ViewControllerA's methods, you can do it like this,

假设您从ViewControllerA实例导航到ViewControllerB实例,并且您希望调用ViewControllerA的方法,您可以这样做,

ViewControllerA * viewControllerA = (ViewControllerA *)self.parentViewController.
[viewController methodToCall];

To use this to address the requirement in the question, you can use a property.

要使用它来解决问题中的要求,您可以使用属性。

viewControllerA.name = firstName.text; // Bits from your code snippet

However in a table view I would expect you to have a mutable array powering the data source. You can refer to it, assuming that it is an property.

但是在表格视图中,我希望你有一个可变数组来驱动数据源。你可以参考它,假设它是一个属性。

[viewControllerA.dataSourceArray addObject:firstName.text];

#1


4  

In ViewControllerB, declare delegate and set action for popViewControllerAnimated:

在ViewControllerB中,为popViewControllerAnimated声明委托和设置动作:

@interface ViewControllerB : UIViewController {
    id delegate;
}

@property (nonatomic, retain) id delegate;

@synthesize delegate;

- (id) init ... {
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                              style:UIBarButtonItemStyleBordered
                                                                             target:self
                                                                             action:@selector(didBack:)] autorelease];
}

- (void) didBack:(id)sender {
    if ([delegate respondsToSelector:@selector(setProperty:)]) { 
        [delegate setProperty:property];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

In ViewControllerA, provide a function to set the local property and set delegate:

在ViewControllerA中,提供一个函数来设置本地属性并设置委托:

ViewControllerB controllerB = [[ViewControllerB alloc] init...];
[controllerB setDelegate:self];
[self.navigationController pushViewController:controllerB animated:YES];
[controllerB release];

#2


1  

You need to store value in one of the global variable for example you can declare in appDelegate file. See this post for that

您需要将值存储在一个全局变量中,例如您可以在appDelegate文件中声明。看到这篇文章

If you are using UITextField then you can store value in above variable from UITextField in below delegate method.

如果您正在使用UITextField,那么您可以在UITextField中将值存储在以下委托方法中。

- (BOOL)textFieldShouldReturn:(UITextField *)textField;

Hope this help.

希望这有帮助。

#3


1  

You could use NSNotificationCenter for this, passing an object along with the call.

您可以使用NSNotificationCenter,将对象与调用一起传递。

Howto: Send and receive messages through NSNotificationCenter in Objective-C?

Howto:通过Objective-C中的NSNotificationCenter发送和接收消息?

#4


0  

Based on your requirement in your comment,

根据您在评论中的要求,

Say you navigate from ViewControllerA instance to ViewControllerB instance and you wish to call ViewControllerA's methods, you can do it like this,

假设您从ViewControllerA实例导航到ViewControllerB实例,并且您希望调用ViewControllerA的方法,您可以这样做,

ViewControllerA * viewControllerA = (ViewControllerA *)self.parentViewController.
[viewController methodToCall];

To use this to address the requirement in the question, you can use a property.

要使用它来解决问题中的要求,您可以使用属性。

viewControllerA.name = firstName.text; // Bits from your code snippet

However in a table view I would expect you to have a mutable array powering the data source. You can refer to it, assuming that it is an property.

但是在表格视图中,我希望你有一个可变数组来驱动数据源。你可以参考它,假设它是一个属性。

[viewControllerA.dataSourceArray addObject:firstName.text];