学习Button第二步

//
//  AppDelegate.m
//  Button
//
//  Created by LQ on 9/26/12.
//  Copyright (c) 2012 Visitor. All rights reserved.
//

#import "AppDelegate.h"
#import "MainViewController.h"
@implementation AppDelegate

- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
   
    //实例化主视图控制器对象
    MainViewController *mvc = [[MainViewController alloc]init];
    //建立导航控制器并设置主视图控制器
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:mvc];
    //把导航控制器加入window中
    [self.window addSubview:nav.view];
   
   
   
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

 

 

//
//  MainViewController.m
//  Button
//
//  Created by LQ on 9/26/12.
//  Copyright (c) 2012 Visitor. All rights reserved.
//

#import "MainViewController.h"
#import "SubViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController
{
    UIButton *_btn2;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
 // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor orangeColor];
    //设置当前导航控制器的标题
    self.navigationItem.title = @"MainView";
    //设置当前导航控制器的样式
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
   
    //生成按钮(用系统自带工厂模式)
    //圆角按钮
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn1.frame = CGRectMake(100, 50, 120, 30);
    //设置按钮上显示的标题
    [btn1 setTitle:@"按钮1" forState:UIControlStateNormal];
    btn1.tag = 1;
    //设置按钮点击响应事件
    [btn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
   
    //加号按钮
    _btn2 = [UIButton buttonWithType:UIButtonTypeContactAdd];
    _btn2.frame = CGRectMake(100, 150, 120, 30);
    //给按钮加上标签,区分按钮
    _btn2.tag = 2;
    [_btn2 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_btn2];
   
    //自定义按钮
    UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn3.frame = CGRectMake(110, 200, 100, 50);
    //给自定义按钮加图片
    [btn3 setImage:[UIImage imageNamed:@"freewolf.jpg"] forState:UIControlStateNormal];
    btn3.tag = 3;
    [btn3 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn3];
   
    //箭头按钮
    UIButton *btn4 = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    btn4.frame = CGRectMake(110, 280, 100, 30);
    btn4.tag = 4;
    [btn4 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn4];
   
   
   
}

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

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

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

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

//按钮点击事件
- (void)btnClick:(UIButton *)btn
{
    if(btn.tag == 1)
    {
        SubViewController *svc = [[SubViewController alloc]init];
        //操作导航控制器进行试图控制器的切换
        [self.navigationController pushViewController:svc animated:YES];
    }
    else if(btn.tag == 2)
    {
        NSLog(@"按钮2触发事件");
    }
    else if(btn.tag == 3)
    {
        //按钮隐藏
        _btn2.hidden = YES;
    }
    else
    {
        _btn2.hidden = NO;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

 

//
//  SubViewController.m
//  Button
//
//  Created by LQ on 9/26/12.
//  Copyright (c) 2012 Visitor. All rights reserved.
//

#import "SubViewController.h"

@interface SubViewController ()

@end

@implementation SubViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
 // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor purpleColor];
    self.navigationItem.title = @"SubView";
   
    //信息按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeInfoLight];
    btn.frame = CGRectMake(100, 200, 120, 30);
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
   
}

- (void)btnClick
{
    //弹出当前试图控制器
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

 

 

 

 

 

 

 

 

 

 

 


 

转载于:https://my.oschina.net/u/811475/blog/80396

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值