zoukankan      html  css  js  c++  java
  • 分栏控制器2

     

             

    #import "MyTabBarController.h"

    #import "LoginViewController.h"

    #import "BViewController.h"

    #import "FirstViewController.h"

    #import "DViewController.h"

     

    @interface MyTabBarController ()

     

    @end

     

    @implementation MyTabBarController

     

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

        

        [self createItem];

    }

     

    //创建视图控制器

    - (void)createViewControllers

    {

        //注册信息页

        LoginViewController *av = [[LoginViewController alloc]init];

        UINavigationController *an = [[UINavigationController alloc]initWithRootViewController:av];

        

        //随机数页面不用导航

        BViewController *bv = [[BViewController alloc]init];

        

        //三个button页

        FirstViewController *cv = [[FirstViewController alloc]init];

        UINavigationController *cn = [[UINavigationController alloc]initWithRootViewController:cv];

        

        //设置导航页(人人的导航条)

        DViewController *dv = [[DViewController alloc]init];

        UINavigationController *dn = [[UINavigationController alloc]initWithRootViewController:dv];

        

        NSArray *arr = [NSArray arrayWithObjects:an,bv,cn,dn, nil];

        

        self.viewControllers = arr;

    }

     

    //自定义tabBar的外观

    - (void)createItem

    {

        UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 49)];

        iv.image = [UIImage imageNamed:@"tab_bg"];

        

        //让imageView接收点击事件

        iv.userInteractionEnabled = YES;

        

        //title数组

        NSArray *titleArr = [NSArray arrayWithObjects:@"登录",@"随机数",@"3个按钮",@"设置", nil];

        

        for (int i = 0; i<4; i++) {

            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

            btn.frame = CGRectMake(25+80*i, 3, 30, 30);

            btn.tag = 1000+i;

            [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_%d",i]] forState:UIControlStateNormal];

            [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_c%d",i]] forState:UIControlStateSelected];

            

            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

            [iv addSubview:btn];

            

            UILabel *la = [[UILabel alloc]initWithFrame:CGRectMake(-5, 33, 40, 12)];

            la.textColor = [UIColor grayColor];

            la.tag = 2000+i;

            la.textAlignment = NSTextAlignmentCenter;

            la.text = [titleArr objectAtIndex:i];

            la.font = [UIFont systemFontOfSize:10];

            [btn addSubview:la];

            

            if (!i) {

                btn.selected = YES;

                la.textColor = [UIColor orangeColor];

            }

        }

        [self.tabBar addSubview:iv];

    }

     

    - (void)btnClick:(UIButton *)sender

    {

        

        for (int i = 0; i<4; i++) {

            UIButton *btn = (UIButton *)[self.tabBar viewWithTag:1000+i];

            btn.selected = NO;

            

            UILabel *la = (UILabel *)[self.tabBar viewWithTag:2000+i];

            la.textColor = [UIColor grayColor];

        }

        

        sender.selected = YES;

        UILabel *la = (UILabel *)[self.tabBar viewWithTag:sender.tag+1000];

        la.textColor = [UIColor orangeColor];

        

        self.selectedIndex = sender.tag - 1000;

    }

     

    #import "LoginViewController.h"

    #import "RegistViewController.h"

     

     

    #import "AppDelegate.h"

     

    @interface LoginViewController () <sendValueDelegate>

     

    {

        UILabel *_label;

    }

     

    @end

     

    @implementation LoginViewController

     

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

        

        self.navigationController.navigationBar.translucent = NO;

        

        self.navigationItem.title = @"登录";

        

        CGFloat s_height = [[UIScreen mainScreen]bounds].size.height;

        

        _label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, s_height-64-49-30)];

        _label.font = [UIFont boldSystemFontOfSize:20];

        _label.numberOfLines = 0;

        //如果不给label设个文字,那么_label.text就是空指针

        _label.text = @"";

        [self.view addSubview:_label];

        

        UIButton *registBtn = [[UIButton alloc]initWithFrame:CGRectMake(110,s_height-64-49-30, 100, 30)];

        [registBtn setTitle:@"添加信息" forState:UIControlStateNormal];

        registBtn.backgroundColor = [UIColor grayColor];    [registBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:registBtn];

    }

     

    //button点击事件

    - (void)btnClick:(UIButton *)sender

    {

        //创建一个想要弹出的新的视图的实例

        RegistViewController *rvc = [[RegistViewController alloc]init];

        

        //将自己设为第二个页面的代理,这样就能接收代理方法了

        rvc.delegate = self;

        rvc.hidesBottomBarWhenPushed = YES;

        

        [self.navigationController pushViewController:rvc animated:YES];

    }

     

    //代理方法

    - (void)postSomeValue:(NSString *)str

    {

        NSLog(@"str = %@",str);

        

        if (_label.text.length) {

            //如果label里已经有文字了,就先加个换行

            _label.text = [_label.text stringByAppendingString:@" "];

        }

        

        //拼接

        _label.text = [_label.text stringByAppendingString:str];

    }

     

    #import "RegistViewController.h"

     

    @interface RegistViewController () <UITextFieldDelegate>

     

    {

        UITextField *_nametf;

        UITextField *_passtf;

        UITextField *_passAgain;

    }

     

    @end

     

    @implementation RegistViewController

     

    - (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 = @"注册";

        

        _nametf = [[UITextField alloc]initWithFrame:CGRectMake(40, 60, 240, 30)];

        _nametf.placeholder = @"name";

        _nametf.borderStyle = UITextBorderStyleRoundedRect;

        [self.view addSubview:_nametf];

        

        _passtf = [[UITextField alloc]initWithFrame:CGRectMake(40, 120, 240, 30)];

        _passtf.placeholder = @"pass";

        _passtf.borderStyle = UITextBorderStyleRoundedRect;

        [self.view addSubview:_passtf];

        

        _passAgain = [[UITextField alloc]initWithFrame:CGRectMake(40, 180, 240, 30)];

        _passAgain.placeholder = @"again";

        _passAgain.borderStyle = UITextBorderStyleRoundedRect;

        [self.view addSubview:_passAgain];

        

        _nametf.tag = 101;

        _passtf.tag = 102;

        _passAgain.tag = 103;

        

        _nametf.delegate = self;

        _passtf.delegate = self;

        _passAgain.delegate = self;

        

        UIButton *okBtn = [[UIButton alloc]initWithFrame:CGRectMake(190, 300, 60, 30)];

        [okBtn setTitle:@"确定" forState:UIControlStateNormal];

        okBtn.tag = 2;

        [okBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:okBtn];

    }

     

    - (void)btnClick:(UIButton *)sender

    {

        if (_nametf.text.length && _passtf.text.length && _passAgain.text.length) {

            

            if ([_passtf.text isEqualToString:_passAgain.text]) {

                

                //判断代理对象是否可以响应这个函数

                if ([self.delegate respondsToSelector:@selector(postSomeValue:)]) {

                    //把账号和密码传给代理

                    [self.delegate postSomeValue:[NSString stringWithFormat:@"账号:%@,密码:%@",_nametf.text,_passtf.text]];

                }

                

            } else {

                UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"两次密码不一致" message:nil delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];

                [av show];

                return;

            }

            

        } else {

            UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"请完善信息" message:nil delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];

            [av show];

            return;

        }

        

        

        //退出当前视图控制器

        [self.navigationController popViewControllerAnimated:YES];

    }

     

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

    {

        [self.view endEditing:YES];

    }

     

    #pragma mark - tf delegate

    - (BOOL)textFieldShouldReturn:(UITextField *)textField

    {

        if (textField.tag==103) {

            //如果是最后一个输入框,点击右下角时直接收起键盘

            [textField resignFirstResponder];

        } else {

            //如果不是最后一个,就找到他下面的tf

            UITextField *tf = (UITextField *)[self.view viewWithTag:textField.tag+1];

            //激活下面那个tf的输入状态

            [tf becomeFirstResponder];

        }

        return YES;

    }

     

    #import "BViewController.h"

     

    @interface BViewController ()

     

    {

        //时间器的声明

        NSTimer *_time;

    }

     

    @end

     

    @implementation BViewController

     

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

        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 30, 300, 100)];

        label.backgroundColor = [UIColor grayColor];

        label.tag = 1;

        label.text = @"随机数";

        label.font = [UIFont systemFontOfSize:60];

        label.textAlignment = NSTextAlignmentCenter;

        [self.view addSubview:label];

        

        

        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(110, 200, 100, 50)];

        btn.titleLabel.font = [UIFont systemFontOfSize:40];

        

        [btn setTitle:@"开始" forState:UIControlStateNormal];

        [btn setTitle:@"停止" forState:UIControlStateSelected];

        

        btn.backgroundColor = [UIColor cyanColor];

        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

        //设置文字颜色

        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        //selected状态下的文字颜色

        [btn setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];

        [self.view addSubview:btn];

        

    }

     

    - (void)btnClick:(UIButton *)sender

    {

        if (sender.selected) {

            sender.selected = NO;

            

            //停掉这个定时器

            [_time invalidate];

        } else {

            sender.selected = YES;

            

            //在这个地方启动时间器

            //第一个参数是间隔,第二个是执行函数的对象,第三个是函数名,第四个参数一般写nil,最后一个是否重复执行

            _time = [NSTimer scheduledTimerWithTimeInterval:0.01f target:self selector:@selector(timeFunc) userInfo:nil repeats:YES];

        }

    }

     

    - (void)timeFunc

    {

        //从self.view中,找到tag值设为1的那个view

        UILabel *newLabel = (UILabel *)[self.view viewWithTag:1];

        

        int a = arc4random_uniform(100);

        

        newLabel.text = [NSString stringWithFormat:@"%d",a];

    }

     

    #import "FirstViewController.h"

     

    @interface FirstViewController ()

     

    @end

     

    @implementation FirstViewController

     

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

        [self.navigationController.navigationBar setBarTintColor:[UIColor redColor]];

        self.navigationController.navigationBar.translucent = NO;

        self.navigationItem.title = @"按钮一";

        

        UIButton *pushButton = [UIButton buttonWithType:UIButtonTypeCustom];

        pushButton.frame = CGRectMake(120, 70, 80, 40);

        pushButton.backgroundColor = [UIColor grayColor];

        [pushButton setTitle:@"push" forState:UIControlStateNormal];

        [pushButton addTarget:self action:@selector(pushButtonClick) forControlEvents:UIControlEventTouchUpInside];

        [pushButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];

        [self.view addSubview:pushButton];

    }

     

    - (void)pushButtonClick

    {

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

        svc.postStr = self.navigationItem.title;

        svc.delegate = self;

        

        svc.hidesBottomBarWhenPushed = YES;

        [self.navigationController pushViewController:svc animated:YES];

    }

     

    -(void)postString:(NSString *)str

    {//收到代理方法以后设置title

        self.navigationItem.title = str;

    }

     

    #import "SecondViewController.h"

     

    @interface SecondViewController ()

     

    @end

     

    @implementation SecondViewController

     

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

        

        NSArray *titleArr = @[@"按钮一",@"按钮二",@"按钮三"];

        for (int i = 0; i < 3; i++) {

            UIButton *bu = [UIButton buttonWithType:UIButtonTypeCustom];

            bu.frame = CGRectMake(130, 80+i*40, 60, 40);

            bu.tag = 10+i;

            [bu setTitle:[titleArr objectAtIndex:i] forState:UIControlStateNormal];

            

            //通过上一个页面传过来的str来确定button的状态

            if ([self.postStr isEqualToString:[titleArr objectAtIndex:i]]) {

                [bu setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

            } else {

                [bu setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

            }

            

            [bu addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

            [self.view addSubview:bu];

        }

    }

     

    - (void)buttonClick:(UIButton *)sender

    {

        //点击的时候给代理发送一个消息,将button的title字符串传过去

        

        [self.delegate postString:sender.currentTitle];

        [self.navigationController popViewControllerAnimated:YES];

    }

     

    #import "DViewController.h"

     

    @interface DViewController ()

     

    @end

     

    @implementation DViewController

     

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

        

        self.navigationController.navigationBar.barTintColor = [UIColor redColor];

        

        //设置导航条的titleView

        UIImageView *titleIV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 60, 35)];

        titleIV.image = [UIImage imageNamed:@"logo_title"];

        self.navigationItem.titleView = titleIV;

        

        //先创建一个button

        UIButton *leftB = [UIButton buttonWithType:UIButtonTypeSystem];

        leftB.frame = CGRectMake(0, 0, 33, 30);

        [leftB setBackgroundImage:[UIImage imageNamed:@"main_left_nav"] forState:UIControlStateNormal];

        [leftB addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

        leftB.tag = 1;

        //用这个button创建一个UIBarButtonItem

        UIBarButtonItem *leftBar = [[UIBarButtonItem alloc] initWithCustomView:leftB];

        //设置左边的专用按钮

        self.navigationItem.leftBarButtonItem = leftBar;

        

        //右边专用按钮

        UIButton *rightB = [UIButton buttonWithType:UIButtonTypeSystem];

        rightB.frame = CGRectMake(0, 0, 48, 29);

        [rightB setBackgroundImage:[UIImage imageNamed:@"main_right_nav"] forState:UIControlStateNormal];

        [rightB addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

        rightB.tag = 2;

        UIBarButtonItem *rightBar = [[UIBarButtonItem alloc] initWithCustomView:rightB];

        self.navigationItem.rightBarButtonItem = rightBar;

    }

     

    - (void)buttonClick:(UIButton *)sender

    {

        NSString *str = nil;

        if (sender.tag == 1) {

            str = @"左边";

        } else {

            str = @"右边";

        }

        

        UIAlertView *av = [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:@"点了%@按钮",str] message:nil delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];

        [av show];

    }

     

    让明天,不后悔今天的所作所为
  • 相关阅读:
    Maven关于web.xml中Servlet和Servlet映射的问题
    intellij idea的Maven项目运行报程序包找不到的错误
    修改Maven项目默认JDK版本
    刷题15. 3Sum
    刷题11. Container With Most Water
    刷题10. Regular Expression Matching
    刷题5. Longest Palindromic Substring
    刷题4. Median of Two Sorted Arrays
    刷题3. Longest Substring Without Repeating Characters
    刷题2. Add Two Numbers
  • 原文地址:https://www.cnblogs.com/-yun/p/4379077.html
Copyright © 2011-2022 走看看