UIViewController

本文深入探讨了iOS开发中的Swift编程语言,分享了Swift语言的特性、开发环境配置、常用框架使用及优化技巧,旨在帮助开发者提升iOS应用开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import "AppDelegate.h"

#import "MainViewController.h"

@interface AppDelegate ()


@end


@implementation AppDelegate


- (void)dealloc

{

    [_window release];

    [super dealloc];



}


- (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];

    [self.window makeKeyAndVisible];

    [_window release];

    

    // UIViewController的使用

    

    // 1.UIViewController的一个对象代表一个页面,负责处理一部分功能(登入页面,注册页面,找回密码页面...分别就是不同的viewController)

    

    // 2.UIViewController是一个抽象类,,不能直接使用,如果要使用需要创建它的子类才能使用

    

    MainViewController *mainVC = [[MainViewController alloc] init];

    // window设置一个根视图控制器,作为启动之后默认显示的界面

    self.window.rootViewController = mainVC;

    [mainVC release];

    

    return YES;

}



#import "MainViewController.h"

#import "SecondViewController.h"

@interface MainViewController ()


@end


@implementation MainViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    // 以后的视图创建代码写在这里

    

    // 每一个视图控制器都自带一个view,用来添加其他视图

    self.view.backgroundColor = [UIColor redColor];

    

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

    button.backgroundColor = [UIColor yellowColor];

    button.frame = CGRectMake(20, 20, 335, 40);

    [button setTitle:@"button" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

    

    

}


- (void)buttonAction:(UIButton *)btn

{


    NSLog(@"点击");

    

    // 弹出新的一个页面(viewController)

    

    // 1.创建新页面的对象

    SecondViewController *secondVC = [[SecondViewController alloc] init];

    

    // 2.弹出(模态)

    

    // 参数1: 要弹出新页面(viewController)

    // 参数2: 是否需要动画效果

    // 参数3: 弹出完毕之后执行的代码

    

    //secondVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

    

    [self presentViewController:secondVC animated:YES completion:^{

        

        NSLog(@"弹出完毕");

    }];

    

    // 3. 内存管理

    

    [secondVC release];


}


#import "SecondViewController.h"


@interface SecondViewController ()


@end


@implementation SecondViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    

    self.view.backgroundColor = [UIColor blueColor];

    

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

    button.backgroundColor = [UIColor yellowColor];

    button.frame = CGRectMake(20, 20, 335, 40);

    [button setTitle:@"返回" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

    

    

}


- (void)buttonAction:(UIButton *)btn

{


    // 在第二个页面点击返回

    

    [self dismissViewControllerAnimated:YES completion:^{

        

    }];

    


}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值