2014-10-20 117 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

相关问题