1

我想將切換ViewControllers的代碼放在一個名爲SwitchController的類中。但沒有任何反應。切換ViewControllers,但沒有任何反應?

下面是代碼:

//AppDelegate.m

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
     // Override point for customization after application launch. 
     self.window.backgroundColor = [UIColor whiteColor]; 

     RootViewController *root = [[RootViewController alloc] init]; 
     [self.window setRootViewController:root]; 

     [self.window makeKeyAndVisible]; 
     return YES; 
} 

//SwitchController.h

@interface SwitchController : NSObject 

@property (strong,nonatomic) RootViewController *rootViewController; 

+(SwitchController *)sharedInstance; 
-(void)requestToSwitchViewController:(NSInteger)tag; 

@end 

//SwitchController.m

#import "SwitchController.h" 
#import "FirstViewController.h" 
#import "SecondViewController.h" 

@implementation SwitchController 

@synthesize rootViewController = _rootViewController; 

-(id)init 
{ 
    self = [super init]; 
    if (self == nil) 
    { 
     _rootViewController = [[RootViewController alloc] init]; 
    } 
    return self; 
} 

+(SwitchController *)sharedInstance 
{ 
    static SwitchController *sharedSingleton = nil; 
@synchronized([SwitchController class]) 
{ 
    if(sharedSingleton == nil) 
    { 
     sharedSingleton = [[self alloc] init]; 
    } 
} 
return sharedSingleton; 
} 

-(void)requestToSwitchViewController:(NSInteger)tag 
{ 
    switch (tag) 
    { 
     case 1: 
     { 
      FirstViewController *first = [[FirstViewController alloc] init]; 
      [self.rootViewController presentViewController:first animated:YES completion:nil]; 
     } 
      break; 
     ......... //Do something else 
     default: 
      break; 
    } 
} 

@end 

//這是self.window.rootV iewController ------ RootViewController.m

//在這個視圖控制器的觀點,有一個按鈕,並在報刊,它會做以下事情

- (IBAction)pressed:(id)sender 
    { 
     [[SwitchController sharedInstance] requestToSwitchViewController:[(UIButton *)sender tag]]; 
    } 

那麼,有沒有什麼毛病我的代碼?

爲什麼按下按鈕時什麼都不會發生?

回答

0

我想你只是定義viewController,你也需要定義View。他們是不同的東西。

1

可能有幾個問題..

1)檢查您的按鈕,電源插座是否正確連接到這種方法-(IBAction)pressed:(id)sender;

2)添加@synthesize後,這條線在SwitchController.m

static SwitchController *sharedSingleton = nil;

3)改變你的方法這樣

+(SwitchController *)sharedInstance 
{ 
    if(sharedSingleton == nil) 
    { 
     sharedSingleton = [[self allocWithZone:NULL] init]; 
    } 
    return sharedSingleton; 
} 

4)從-id(init)方法

// You do not need to re-initialize the root view controller 
_rootViewController = [[RootViewController alloc] init]; 

5)檢查按鈕是否有效除去此線。調用此方法requestToSwitchViewController:檢查什麼,它記錄

更新之前:

嘗試這樣的:(對於這一點,你需要create a property for navigation controllerappDelegate和合成它)

switch (tag) 
{ 
    case 1: 
    { 
     FirstViewController *first = [[FirstViewController alloc] init]; 
     appDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
     [appDelegate.navigationController presentViewController:first animated:YES completion:nil]; 
    } 
    ..... 
    ......... 
} 
+0

謝謝! 1.我檢查了按鈕插座連接正確。我已經完成了你的建議; 3.我設置了幾個斷點,它可以運行到「[self.rootViewController presentViewController:first animated:YES completion:nil]」中的斷點行;仍然沒有任何反應 – lixiaoyu

+0

我有一個問題。爲什麼不嘗試在你的RootViewController類中編寫你的requestToSwitchController:方法。因爲UI轉換隻能在viewController類中處理。你正試圖從你的NSObject文件中呈現模態視圖控制器。我認爲這個鏈接會幫助你。 HTTP://計算器。com/questions/3760286/call-presentmodalviewcontroller-from-nsobject-class –

+0

檢查我的更新答案。 –

相關問題