1

ViewController를 SwitchController라는 단일 클래스로 전환하는 코드를 넣고 싶습니다. 그러나 아무 일도 일어나지 않습니다. 여기 ViewController를 전환하지만 아무 일도 일어나지 않습니까?

코드이다

//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 ------

//이의 ViewController의 관점에서 RootViewController.m

는, 버튼이 키를 누릅니다에, 그것은 다음과 같은 일이 그래서

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

을 할 것입니다,에 문제가 있나요 내 코드?

버튼을 눌러도 아무런 반응이 없나요?

답변

0

viewController를 정의하는 것 같아요. 뷰도 정의해야합니다. 그들은 다른 것들입니다.

1

1) 버튼 콘센트가 올바르게 SwitchController.m

static SwitchController *sharedSingleton = nil;

이 방법 -(IBAction)pressed:(id)sender;

2) @synthesize 후이 줄을 추가로 연결되어 있는지 확인 .. 몇 가지 문제가있을 수 있습니다

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) 버튼이 유효한지 여부를 검사에서이 선을 제거한다. 이

UPDATE를 기록을 확인할 requestToSwitchViewController:이 메서드를 호출하기 전에 :

다음과 같이하십시오 : (이 경우, 당신은 당신의 appDelegatecreate a property for navigation controller 필요하고 그것을 종합)

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. 버튼 콘센트가 올바르게 연결되었는지 확인했습니다. 2. 나는 당신이 제안한 것을했습니다. 3. 여러 개의 중단 점을 설정했으며 "[self.rootViewController presentViewController : first animated : YES completion : nil];"의 중단 점 행까지 실행할 수 있습니다. 그래도 아무 일도 없었어요 – lixiaoyu

+0

질문이 있습니다. 왜 RootViewController 클래스에서 requestToSwitchController : 메소드를 작성하려고하지 않습니까? UI 전환은 viewController 클래스에서만 처리되어야하기 때문입니다. NSObject 파일에서 모달보기 컨트롤러를 표시하려고합니다. 나는이 링크가 당신을 도울 것이라고 생각합니다. http : // stackoverflow.com/questions/3760286/call-presentmodalviewcontroller-from-nsobject-class –

+0

업데이트 된 답변을 확인하십시오. –

관련 문제