如何访问UINavigationController中的先前视图元素 [英] how to access previous view elements in UINavigationController

查看:44
本文介绍了如何访问UINavigationController中的先前视图元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UINavigationController,我在其中加载了不同的视图控制器.我想知道如何访问上一个视图的元素(如标签等).这是一个例子.

I have a UINavigationController in which I am loading different view controllers. I want to know how can i access the elements (like labels etc) of my previous view. Here is an eg.

查看AmyLabel.text = @第一视图";

View A myLabel.text = @"first view";

(用户移至视图B)

查看B(用户输入了一条消息,我需要在视图A中显示该消息)类似于ViewA.myLabel.text = @用户输入的消息"

View B (user entered a message, that i need to display in View A) something like ViewA.myLabel.text = @"user entered message"

我尝试了很多事情,但是找不到非常有用的东西.请帮忙.

I tried many things but was not able to find anything very useful. Please help..

我正在使用没有ARC和故事板的Xcode 4.

I am using Xcode 4 without ARC and without storyboard.

谢谢山姆

我想更新在View A的viewController中声明的属性,而不是直接更新标签.我的标签使用该属性更新.就像按下viewController一样,我们可以按如下所示传递值.

I want to update the property declared in viewController of View A and not the labels directly. My labels get updated using that property. Like while pushing the viewController we can pass the values as below.

ViewA *myView = [[ViewA alloc] init];
myView.title = @"View B" ;
myView.tableView.tag = 3;
myView.myTextView.text = @"Some Text";
[self.navigationController pushViewController:myView animated:YES];
[myView release];

在弹出ViewB并返回到ViewA时,是否可以将这些值传递给ViewA的ViewController属性?

Is there any way to pass these values to properties of ViewController of ViewA while popping ViewB and returning back to ViewA ?

实际情况如下:用户获得并选择在textView中编写消息,或者他可以使用预定义的模板.如果他单击模板按钮,则将转到预定义模板列表,从中可以选择任何预定义消息.现在,我希望当用户单击任何预定义消息时,将弹出包含预定义消息列表的视图,并自动在主视图的textView中填充他选择的消息.实现此目标的最佳方法是什么?

The actual scenario is as follows: the user gets and option to write a message in textView or he can use the predefined templates. If he clicks on the templates button he is taken to a list of predefined templates where he can select any of the predefined message. Now I want that when the user click on any of the predefined message the view containing the list of predefined message gets popped of and the message he selected gets automatically populated in the textView of main view. what is the best approach to achieve this ?

TIA山姆

推荐答案

您应该将AViewController设置为BViewController的委托,以便可以在特定事件后将其发送回消息.使用委托还可以使ViewController更好地分离.

You should set your AViewController as the delegate of your BViewController so you can message it back after a particular event. Using a delegate will also allow better decoupling of your ViewControllers.

在您的BViewController中,定义如下协议:

In your BViewController, define a protocol like this :

BViewController.h:

BViewController.h :

@protocol BViewControllerDelegate <NSObject>
- (void)viewB:(UIViewController *)didEnterMessage:(NSString *)message;
@end

并添加一个委托属性:

@property (nonatomic, weak) id <BViewControllerDelegate> delegate;

当用户在您的BViewController中输入消息并点击弹出BViewController以显示给AViewController的按钮时,请执行以下操作:

When the user enter the message in your BViewController and hit the button that pops the BViewController to show to AViewController do this :

- (IBAction)messageEntered {
  if ([self.delegate respondsToSelector:@selector(viewB:didEnterMessage:)]) {
    [self.delegate viewB:self didEnterMessage:self.yourTextField.text];
  }
}

您的AViewController应该实现BViewControllerDelegate协议,如下所示:

Your AViewController should implement the BViewControllerDelegate protocol like this :

AViewController.h:

AViewController.h :

@interface AViewController <BViewControllerDelegate>

当您的AViewController创建BViewController时,在呈现它之前应将其设置为其委托.可能看起来像这样:

When your AViewController creates the BViewController, it should set itself as its delegate before presenting it. Might look like this :

BViewController *bvc = [[BViewController alloc] init…];
bvc.delegate = self;

最后,您的AViewController应该实现viewB:didEnterMessage:方法:

And finally, your AViewController should implement the viewB:didEnterMessage: method :

- (void)viewB:(UIViewController *)didEnterMessage:(NSString *)message {
  self.myLabel.text = message;
}

这是最干净的方法,恕我直言.

That's the cleanest way to do that, IMHO.

这篇关于如何访问UINavigationController中的先前视图元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆