2014-10-20 119 views
0

我已經看了全部谷歌&堆棧溢出,但我還沒有找到一個明確的答案。點擊UIButton後如何切換到TOCViewController?我目前使用「presentViewController」,但它只是點擊按鈕凍結。預先感謝您的幫助。如何在iOS中切換視圖控制器?

#import "SplashLoginViewController.h" 
#import <QuartzCore/QuartzCore.h> 
#import "TOCViewController.h" 

@interface SplashLoginViewController() 

@end 

@implementation SplashLoginViewController 

- (void) displayTOC { 
    TOCViewController *toc = [[TOCViewController alloc] init]; 
    [self presentViewController:toc animated:YES completion:nil]; 
} 


- (void)initializeSplashView 
{ 
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.view.bounds.size.height)]; 
    backgroundView.backgroundColor = [UIColor colorWithRed:52.0/255.0 green:170.0/255.0 blue:220.0/255.0 alpha:1.0]; 
    backgroundView.tag = 1; 
    backgroundView.layer.zPosition = 99.0f; 
    [self.view addSubview:backgroundView]; 

    UILabel *codeworksLabelView = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width, self.view.bounds.size.height/2.0 - 25.0, self.view.bounds.size.width, 50.0)]; 
    codeworksLabelView.text = @"Codeworks"; 
    codeworksLabelView.textAlignment = NSTextAlignmentCenter; 
    codeworksLabelView.textColor = [UIColor whiteColor]; 
    codeworksLabelView.font = [UIFont fontWithName:@"Montserrat" size:30.0]; 
    codeworksLabelView.tag = 2; 
    codeworksLabelView.layer.zPosition = 100.0f; 
    [self.view addSubview:codeworksLabelView]; 
} 

- (void)initializeLoginView 
{ 
    CGRect loginFrame = CGRectMake(50.0, 100.0, self.view.bounds.size.width - 100.0, 30.0); 
    CGRect passwordFrame = CGRectMake(50.0, 180.0, self.view.bounds.size.width - 100.0, 30.0); 

    UITextField *usernameInput = [[UITextField alloc] init]; 
    usernameInput.frame = loginFrame; 
    usernameInput.placeholder = @"Username"; 
    usernameInput.font = [UIFont fontWithName:@"Montserrat" size:15.0]; 
    usernameInput.returnKeyType = UIReturnKeyNext; 
    usernameInput.tag = 3; 
    usernameInput.autocorrectionType = UITextAutocorrectionTypeNo; 

    UITextField *passwordInput = [[UITextField alloc] init]; 
    passwordInput.frame = passwordFrame; 
    passwordInput.placeholder = @"Password"; 
    passwordInput.font = [UIFont fontWithName:@"Montserrat" size:15.0]; 
    passwordInput.returnKeyType = UIReturnKeyDone; 
    passwordInput.tag = 4; 
    passwordInput.secureTextEntry = YES; 

    CALayer *bottomBorder = [CALayer layer]; 
    bottomBorder.frame = CGRectMake(0.0, 29.0, usernameInput.frame.size.width, 1.0); 
    bottomBorder.backgroundColor = [UIColor blackColor].CGColor; 
    CALayer *borderCopy = [CALayer layer]; 
    borderCopy.frame = CGRectMake(0.0, 29.0, passwordInput.frame.size.width, 1.0); 
    borderCopy.backgroundColor = [UIColor blackColor].CGColor; 

    [usernameInput.layer addSublayer:bottomBorder]; 
    [passwordInput.layer addSublayer:borderCopy]; 

    [self.view addSubview:usernameInput]; 
    [self.view addSubview:passwordInput]; 

    UILabel *signInButton = [[UILabel alloc] initWithFrame:CGRectMake(0, 270, self.view.bounds.size.width, 60.0)]; 
    signInButton.font = [UIFont fontWithName:@"Montserrat" size:15.0]; 
    signInButton.text = @"Sign In"; 
    signInButton.textColor = [UIColor blackColor]; 
    signInButton.tag = 5; 
    signInButton.textAlignment = NSTextAlignmentCenter; 

    CALayer *topButtonBorder = [CALayer layer]; 
    topButtonBorder.frame = CGRectMake(0, 0, self.view.bounds.size.width, 1); 
    topButtonBorder.backgroundColor = [UIColor blackColor].CGColor; 

    CALayer *bottomButtonBorder = [CALayer layer]; 
    bottomButtonBorder.frame = CGRectMake(0, 59, self.view.bounds.size.width, 1); 
    bottomButtonBorder.backgroundColor = [UIColor blackColor].CGColor; 

    [signInButton.layer addSublayer:topButtonBorder]; 
    [signInButton.layer addSublayer:bottomButtonBorder]; 

    [self.view addSubview:signInButton]; 

    UIView *optionsDivide = [[UIView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/2 - 0.5, self.view.bounds.size.height - 45, 1, 20)]; 
    optionsDivide.backgroundColor = [UIColor blackColor]; 
    [self.view addSubview:optionsDivide]; 

    UIButton *tos = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    UIButton *pp = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

    tos.frame = CGRectMake(self.view.bounds.size.width/2 - 115.5 , self.view.bounds.size.height - 65, 110, 60); 
    pp.frame = CGRectMake(self.view.bounds.size.width/2 + 0.5 , self.view.bounds.size.height - 65, 110, 60); 

    [tos setTitle:@"Terms of Service" forState:UIControlStateNormal]; 
    [pp setTitle:@"Privacy Policy" forState:UIControlStateNormal]; 

    pp.titleLabel.textAlignment = NSTextAlignmentLeft; 

    tos.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:12.0]; 
    pp.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:12.0]; 

    [tos addTarget:self action:@selector(displayTOC:) forControlEvents:UIControlEventTouchUpInside]; 

    [self.view addSubview:tos]; 
    [self.view addSubview:pp]; 

} 

- (void)animateSplashScreen 
{ 
    [UIView animateWithDuration:1.0 
          delay:0.0 
         options:UIViewAnimationOptionCurveEaseInOut 
        animations:^{ 
         [self.view viewWithTag:2].frame = CGRectMake(0.0, self.view.bounds.size.height/2.0 - 25.0, self.view.bounds.size.width, 50.0); 
        } 
        completion:^(BOOL finished){ 
         [self initializeLoginView]; 
         [UIView animateWithDuration:0.5 
               delay:1.0 
              options:UIViewAnimationOptionCurveEaseOut 
              animations:^{ 
               [self.view viewWithTag:1].frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, 65.0); 
               [self.view viewWithTag:2].frame = CGRectMake(0.0, 15.0, self.view.bounds.size.width, 50.0); 

               ((UILabel *)[self.view viewWithTag:2]).transform = CGAffineTransformScale(((UILabel *)[self.view viewWithTag:2]).transform, 0.6, 0.6); 
              } 
              completion:^(BOOL finished){ 
               ((UILabel *)[self.view viewWithTag:2]).font = [UIFont fontWithName:@"Montserrat" size:30.0]; 

              }]; 
        }]; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self initializeSplashView]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
    [self animateSplashScreen]; 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    //CGPoint locationPoint = [[touches anyObject] locationInView:self.view]; 
    //CGPoint viewPoint = [[self.view viewWithTag:5] convertPoint:locationPoint fromView:self.view]; 
    //if ([[self.view viewWithTag:5] pointInside:viewPoint withEvent:event]) { 
    // [self.view viewWithTag:5].frame = CGRectMake(self.view.bounds.size.width/2 - 62.5, 254.5, 130.0, 60.0); 
    //} 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    //[self.view viewWithTag:5].frame = CGRectMake(self.view.bounds.size.width/2 - 67.5, 249.5, 130.0, 60.0); 
} 

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    //[self.view viewWithTag:5].frame = CGRectMake(self.view.bounds.size.width/2 - 67.5, 249.5, 130.0, 60.0); 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
} 

@end 
+0

要回答這個問題,我需要知道您是否在項目中使用.storyboard文件。 – 2014-10-20 01:21:24

+0

@DanielT。我不認爲我是。我有一個故事板,但它是空的,因爲我已經通過編程添加了所有元素。 – jak1200 2014-10-20 01:47:40

回答

0

我複製粘貼代碼到一個文件,發現在這條線[tos addTarget:self action:@selector(displayTOC:) forControlEvents:UIControlEventTouchUpInside];警告說,一個警告,

Undeclared selector 'displayTOC:'

當我運行的代碼,並拍了拍TOC按鈕,它沒有隻是凍結,它與錯誤墜毀:

-[ViewController displayTOC:]: unrecognized selector sent to instance ...

這些應該是你的代碼有什麼問題的強烈提示。當你向人們詢問你的代碼時,你應該透露任何和所有的警告和崩潰。

注意,一個名爲displayTOC方法是不一樣的一個名爲displayTOC:

順便說方法,你做動畫是真的很酷。做得好!

+0

現在我可能已經很明顯地知道我對iOS很新。你能詳細說明如何解決它嗎?我只是刪除冒號?另外,謝謝,我也非常喜歡動畫。 – jak1200 2014-10-20 02:28:28

+0

我剛剛嘗試刪除冒號,它的工作。謝謝! – jak1200 2014-10-20 03:21:26

相關問題