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]];
}
을 할 것입니다,에 문제가 있나요 내 코드?
버튼을 눌러도 아무런 반응이 없나요?
감사합니다! 1. 버튼 콘센트가 올바르게 연결되었는지 확인했습니다. 2. 나는 당신이 제안한 것을했습니다. 3. 여러 개의 중단 점을 설정했으며 "[self.rootViewController presentViewController : first animated : YES completion : nil];"의 중단 점 행까지 실행할 수 있습니다. 그래도 아무 일도 없었어요 – lixiaoyu
질문이 있습니다. 왜 RootViewController 클래스에서 requestToSwitchController : 메소드를 작성하려고하지 않습니까? UI 전환은 viewController 클래스에서만 처리되어야하기 때문입니다. NSObject 파일에서 모달보기 컨트롤러를 표시하려고합니다. 나는이 링크가 당신을 도울 것이라고 생각합니다. http : // stackoverflow.com/questions/3760286/call-presentmodalviewcontroller-from-nsobject-class –
업데이트 된 답변을 확인하십시오. –