简体   繁体   English

从不同的ViewController声明对象

[英]Declaring Objects from different ViewController

In my application, I have a ViewController (ViewController 1) that has a UITextField and a UIButton. 在我的应用程序中,我有一个具有UITextField和UIButton的ViewController(ViewController 1)。 In my ViewController 2, I have a UINavigationBar. 在我的ViewController 2中,我有一个UINavigationBar。 I want to be able to type something in the ViewController 1 textfield, press the button, then have the UINavigationBar text in ViewController 2 be set as the UITextField from ViewController 1. But of course, Xcode will not let you declare objects from another ViewController without doing more work. 我希望能够在ViewController 1文本字段中键入某些内容,然后按一下按钮,然后将ViewController 2中的UINavigationBar文本设置为ViewController 1中的UITextField。但是,当然,Xcode不会让您从另一个ViewController中声明对象,而无需做更多的工作。 How can I do this? 我怎样才能做到这一点?

Haven't tested this but the following should work for you: 尚未对此进行测试,但以下内容应为您工作:

navBar.topItem.title = @"title";

OR 要么

self.navigationItem.title=@"title";

@"title" will be replaced with the string variable (in VC2) that was set before pushing VC2 in the navigationcontroller stack.... @“ title”将替换为在将VC2推入navigationcontroller堆栈之前设置的字符串变量(在VC2中)。

cheers 干杯

Your question is not very clear but from what I could understand you want a ViewController 's UIButton to set the NavigationBar 's title of a second ViewController ? 您的问题不是很清楚,但是据我所知,您希望ViewControllerUIButton设置第二个ViewControllerNavigationBar的标题吗?

Seems like a pretty weird behavior to me but here's what I think might help you. 对我来说,这似乎是一种很奇怪的行为,但是我认为这可能对您有所帮助。

On your first ViewController create a protocol/delegate functionality so that when your UIButton is pressed you can call: 在您的第一个ViewController创建一个协议/代理功能,以便在按下UIButton时可以调用:

-(IBAction) changeTitle:(id) sender {    
[self.delegate buttonPressed:txtView.text];
}

And with that send your UITextField 's text to your AppDelegate , or whoever creates these two ViewControllers , and there you will receive that text and send it to your second ViewController . 然后,将UITextField的文本发送到AppDelegate或创建这两个ViewControllers任何人,在那里您将收到该文本并将其发送到第二个ViewController

-(void)buttonPressed:(NSString *)text{
secondViewController.title = text;
} 

Perhaps you will have to implement a method that changes the title in your second ViewController if that line does not work. 如果该行不起作用,则可能必须实现一个方法来更改第二个ViewController的标题。 Hope this is useful for you and that I understood your question. 希望这对您有用,并且我理解您的问题。

Similar to Oscar's answer but if this is a string you want multiple view controllers to have access to then you might set a property in your app delegate. 与Oscar的答案类似,但是如果这是一个字符串,则希望多个视图控制器可以访问,则可以在应用程序委托中设置一个属性。

In AppDelegate
@property (NSString *) myString;

In View Controller setting
myAppDelegate *appDelegate =  (myAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.myString = @"meh"; 

In View Controller getting
myAppDelegate *appDelegate=  (myAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *mehStr = appDelegate.myString;

It's not the full code, but you get the idea. 它不是完整的代码,但是您可以理解。 I'm not sure I would go overboard sending stuff back to the appdelegate but for one string it should work fine. 我不确定是否会把东西发送回appdelegate,但是对于一个字符串,它应该可以正常工作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM