如何在iOS中切换View Controllers?

时间:2014-10-20 01:17:43

标签: ios objective-c

我已经遍布谷歌& Stack Overflow,但我还没有找到一个明确的答案。点击UIButton后如何切换到TOCViewController?我目前正在使用" presentViewController"但它只是点击按钮冻结。提前感谢您的帮助。

#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

1 个答案:

答案 0 :(得分:0)

我将您的代码复制粘贴到文件中,并在此行中发现了警告[tos addTarget:self action:@selector(displayTOC:) forControlEvents:UIControlEventTouchUpInside];警告说,

  

未申报的选择器&quot; displayTOC:&#39;

当我运行代码并点击TOC按钮时,它并没有冻结,它因错误而崩溃:

  

- [ViewController displayTOC:]:无法识别的选择器发送到实例...

这些应该是代码中出错的强有力提示。当你向人们询问你的代码时,你应该发现任何和所有的警告,然后崩溃。

注意,名为displayTOC的方法与名为displayTOC:

的方法不同

顺便说一下,你做的动画真的很酷。干得好!