zoukankan      html  css  js  c++  java
  • UIBarButtonItem导航栏添加按钮

    1 前言
    UIBarButtonItem为导航栏按钮,在导航栏的左侧和右侧,他们具有许多种不同的形状和形式。


    2 代码讲解
    ZYViewController.m

    [plain]
     (void)viewDidLoad 

        [super viewDidLoad]; 
        // Do any additional setup after loading the view, typically from a nib. 
        self.view.backgroundColor = [UIColor whiteColor]; 
        self.title = @"First"; 
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Add" style:UIBarButtonItemStylePlain target:self action:@selector(perFormAdd:)];//为导航栏添加右侧按钮 
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(perFormAdd:)];//为导航栏左侧添加系统自定义按钮 

     
    -(void)perFormAdd:(id)paramSender{ 
        NSLog(@"Action method got called."); 

    - (void)viewDidLoad
    {
        [super viewDidLoad];
     // Do any additional setup after loading the view, typically from a nib.
        self.view.backgroundColor = [UIColor whiteColor];
        self.title = @"First";
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Add" style:UIBarButtonItemStylePlain target:self action:@selector(perFormAdd:)];//为导航栏添加右侧按钮
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(perFormAdd:)];//为导航栏左侧添加系统自定义按钮
    }

    -(void)perFormAdd:(id)paramSender{
        NSLog(@"Action method got called.");
    }运行结果:

    当点击左边和右边的按钮的时候,控制台显示:


    2013-04-23 21:40:58.982 UIBarButtonItemTest[660:c07] Action method got called.

    2013-04-23 21:41:02.598 UIBarButtonItemTest[660:c07] Action method got called.


    ZYUIBarButtonViewController.m:


    [plain]
    - (void)viewDidLoad 

        [super viewDidLoad]; 
        // Do any additional setup after loading the view. 
        self.view.backgroundColor = [UIColor whiteColor]; 
        self.title = @"Second"; 
        UISwitch *simpleSwitch = [[UISwitch alloc] init];//实例化一个选择开关 
        simpleSwitch.on = YES;//开关设置为开启状态 
        [simpleSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];//添加事件 
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:simpleSwitch];//将开关控件赋给导航栏右按钮 

     
    -(void)switchChanged:(UISwitch *)paramSender{ 
        if ([paramSender isOn]) {//如果开关状态为开启 
            NSLog(@"Switch is on."); 
        }else{ 
            NSLog(@"Switch is off."); 
        } 

    - (void)viewDidLoad
    {
        [super viewDidLoad];
     // Do any additional setup after loading the view.
        self.view.backgroundColor = [UIColor whiteColor];
        self.title = @"Second";
        UISwitch *simpleSwitch = [[UISwitch alloc] init];//实例化一个选择开关
        simpleSwitch.on = YES;//开关设置为开启状态
        [simpleSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];//添加事件
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:simpleSwitch];//将开关控件赋给导航栏右按钮
    }

    -(void)switchChanged:(UISwitch *)paramSender{
        if ([paramSender isOn]) {//如果开关状态为开启
            NSLog(@"Switch is on.");
        }else{
            NSLog(@"Switch is off.");
        }
    }
    运行结果:

     

    当拨动开关控制台显示:


    2013-04-23 21:46:46.692 UIBarButtonItemTest[727:c07] Switch is off.

    2013-04-23 21:46:47.493 UIBarButtonItemTest[727:c07] Switch is on.

  • 相关阅读:
    Windows 10 搭建Python3 安装使用 protobuf
    [Python爬虫] 在Windows下安装PhantomJS和CasperJS及入门介绍(上)
    [Python爬虫] 在Windows下安装PIP+Phantomjs+Selenium
    [Python爬虫] Selenium自动访问Firefox和Chrome并实现搜索截图
    [Python爬虫] Selenium实现自动登录163邮箱和Locating Elements介绍
    [Python爬虫] Selenium+Phantomjs动态获取CSDN下载资源信息和评论
    [Python爬虫] Selenium获取百度百科旅游景点的InfoBox消息盒
    [Python] 中文编码问题:raw_input输入、文件读取、变量比较等str、unicode、utf-8转换问题
    [python爬虫] Selenium定向爬取海量精美图片及搜索引擎杂谈
    [Python爬虫] scrapy爬虫系列 <一>.安装及入门介绍
  • 原文地址:https://www.cnblogs.com/mawenqiangios/p/5885334.html
Copyright © 2011-2022 走看看