iOS:访问调用 Segue 的同一 UIViewController 实例
<p><p>我有两个 ViewController 。 MainViewController 推送 SignUp ViewController。一旦 SignUp View Controller 关闭,我需要访问 MainViewController 的同一实例来更新 UIButton.title。 </p>
<p>.h 主视图</p>
<pre><code>//MainViewController.h
#import <UIKit/UIKit.h>
@interface MainViewController : UITableViewController
-(void)loggedIn;
@end
</code></pre>
<p>.m</p>
<pre><code>@interface MainViewController ()
@end
-(void)loggedIn
{
NSLog (@"This is Logged in inside MainView.m");
self.logInOutButton.title = @"Logout";
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UIViewController *destinationViewController = segue.destinationViewController;
Signup *signUp = ] ? (Signup*)destinationViewController : nil;
signUp.mainViewController = self;
NSLog(@"Preapre For Segue %@", self);
}
</code></pre>
<p>.h 注册</p>
<pre><code>#import <UIKit/UIKit.h>
#import "MainViewController.h"
@interface SignUp : UIViewController
@property (strong, nonatomic) MainViewController *mainViewController;
@end
</code></pre>
<p>.m</p>
<pre><code>@synthesize mainViewController;
- (void) loggedIn
{
NSLog(@" MainViewController %@", mainViewController);
;
self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
</code></pre>
<p>当我执行 NSLog 时,我得到了不同的值:</p>
<pre><code>Preapre For Seg <STMainViewController: 0x9f7d600>
MainViewController (null)
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>创建协议(protocol)/委托(delegate)。关于如何实现这一点有很多资源。总之,您使用希望在源上运行的方法在目标 viewController 上创建协议(protocol),然后从源 viewController 订阅该委托(delegate)。 </p>
<p> <a href="https://stackoverflow.com/questions/626898/how-do-i-create-delegates-in-objective-c" rel="noreferrer noopener nofollow">Protocol Delegate</a> </p>
<p>不要忘记在您的 prepareForSegue 方法中设置委托(delegate)</p>
<pre><code>- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
Signup *signUp = segue.destinationViewController;
signup.delegate = self;
NSLog(@"Preapre For Segue %@", self);
</code></pre>
<p>}</p></p>
<p style="font-size: 20px;">关于iOS:访问调用 Segue 的同一 UIViewController 实例,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/16359941/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/16359941/
</a>
</p>
页:
[1]