如何使iOS UIViewController成为单身?

时间:2016-02-09 16:23:37

标签: ios objective-c uiviewcontroller singleton

我有两个UIViewController,RootViewController和SecondViewController。

SecondViewController连接到服务器,现在我回到RootViewController(UINavigationController),但当我从RootViewController转向SecondViewController时,有一个新的SecondViewController。

我怎么能转向旧的SecondViewController?

我使用了singleton,但是当我回到RootViewController时,我再也无法转向SecondViewController。

3 个答案:

答案 0 :(得分:0)

应该有一个MainViewController,在MainViewController中你应该初始化RootViewController(或ServerViewController)和SecondViewController。

点击后退按钮时,只需隐藏视图即可。如果您可以共享代码,请执行更多操作。

答案 1 :(得分:0)

AppDelegate.h
@property (strong, nonatomic) UIViewController *sc;
AppDelegate.m
_sc = nil;
RootViewController.m
- (IBAction)tiaozhuian:(UIButton *)sender {
    AppDelegate *appDelegate=[[UIApplication sharedApplication] delegate];
    if(appDelegate.sc == nil)
    {
    appDelegate.sc=[self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
    [self.navigationController pushViewController:appDelegate.sc animated:YES];
    NSLog(@"%@",appDelegate.sc);
    }else{
    [self.navigationController pushViewController:appDelegate.sc animated:YES];
    }
}

//定义一个全局变量sc,并且sc = nil,sc = appDelegate.sc = [self.storyboard instantiateViewControllerWithIdentifier:@“SecondViewController”]当第一次转向SecondView时,返回RootView并再次转向SecondView是零,如果没有,请转到SecondView。

答案 2 :(得分:-2)

AppDelegate.h

@property (strong, nonatomic) UIViewController *sc;

AppDelegate.m

_sc = nil;

RootViewController.m

- (IBAction)tiaozhuian:(UIButton *)sender {
    AppDelegate *appDelegate=[[UIApplication sharedApplication] delegate];
    if(appDelegate.sc == nil)
    {
      appDelegate.sc=[self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
      [self.navigationController pushViewController:appDelegate.sc animated:YES];
      NSLog(@"%@",appDelegate.sc);
    } else {
      [self.navigationController pushViewController:appDelegate.sc animated:YES];
    }
}

定义全局变量sc,sc = nil,sc = appDelegate.sc = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"]

当第一次转向SecondView时,返回RootView并再次转向SecondView,无论sc是否为零,如果没有,请转到SecondView。