Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使iOS UIViewController成为singleton?_Ios_Objective C_Uiviewcontroller_Singleton - Fatal编程技术网

如何使iOS UIViewController成为singleton?

如何使iOS UIViewController成为singleton?,ios,objective-c,uiviewcontroller,singleton,Ios,Objective C,Uiviewcontroller,Singleton,我有两个UIViewController,RootViewController和SecondViewController SecondViewController连接到服务器,现在我返回到RootViewController(UINavigationController),但当我从RootViewController转到SecondViewController时,有一个新的SecondViewController 我怎样才能使用旧的SecondViewController 我使用了singlet

我有两个
UIViewController
,RootViewController和SecondViewController

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

我怎样才能使用旧的SecondViewController


我使用了singleton,但当我返回到RootViewController时,我不能再次转到SecondViewController

应该有一个MainViewController,在MainViewController内部,您应该初始化RootViewController(或ServerViewController)和SecondViewController

当你点击后退按钮时,一个视图应该是隐藏的。如果您可以共享代码,请参阅更多。

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实例化eviewcontrollerwhiteIdentifier:@“SecondViewController”]

当第一次转到SecondView时,返回RootView并再次转到SecondView,无论sc是否为零,如果不是,则转到SecondView

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 instanceeviewcontrollerwhiteIdentifier:@“SecondViewController”]当第一次转到SecondView时,无论sc是否为nil,都返回RootView并再次转到SecondView,如果不是,则转到SecondView。

不要为此使用单例。这是一个糟糕的主意。您应该创建一些帮助器类来实现这一点,就像rmaddy说的:不要为此使用单例!是否有演示?如果我使用helper类,如何连接到UIViewController?helper类是singleton?不要只是发布代码。解释你的答案是如何解决问题的。
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];
    }
}