UITabBarController,UITextView,UIToolbar,UISearchBar的综合应用

//存放视图控制器
    NSMutableArray *_vcs=[NSMutableArray array];
    
    //第一个视图
    AViewController *a=[[AViewController alloc]init];
    //指定navigat的根视图
    UINavigationController *anavi=[[UINavigationController alloc]initWithRootViewController:a];
    //设置tabbar的信息(自定义)
    anavi.tabBarItem.title=@"A";
    anavi.tabBarItem.image=nil;

    anavi.tabBarItem.badgeValue=@"1";

//将nav控制器存放到控制器数组

    [_vcs addObject:anavi];
    [a release];
    [anavi release];

 //系统设置
   /*系统设置
    //UITabBarItem *aItem=[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:100];
    //系统设置有标题
    UITabBarItem *aItem=[[UITabBarItem alloc]initWithTitle:@"A"
                                                     image:nil
                                                       tag:1000];
    anavi.tabBarItem=aItem;
    */
    [_vcs addObject:anavi];
    BViewController *b=[[BViewController alloc]init];
    UINavigationController *bnavi=[[UINavigationController alloc]initWithRootViewController:b];
    bnavi.tabBarItem.title=@"B";
    bnavi.tabBarItem.image=nil;
    bnavi.tabBarItem.badgeValue=@"2";
    


    [_vcs addObject:bnavi];
    [b release];
    [bnavi release];
    UITabBarController *tabBarController=[[UITabBarController alloc]init];
    self.window.rootViewController=tabBarController;
    tabBarController.viewControllers=_vcs;
    tabBarController.delegate=self;
    [tabBarController release];
    return YES;
}
//将要选择

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    //TODO: panduan
    BOOL success=NO;
   //让第四个不能用
//    if ([viewController isEqual:[tabBarController.viewControllers objectAtIndex:3]]) {
//        success=NO;
//    }
    
    return success;
}
//已经选择
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    
}



#import <UIKit/UIKit.h>

@interface AViewController : UIViewController<UISearchBarDelegate>{
   
}

@end


#import "AViewController.h"

@interface AViewController ()

@end

@implementation AViewController

-(void)loadView{
    [super loadView];
    self.title=@"A";
//    UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
//    btn.frame=CGRectMake(100, 100, 100, 100);
//    [btn addTarget:self
//            action:@selector(change:)
//  forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:btn];
    UISearchBar *search=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
    [self.view addSubview:search];
    search.showsCancelButton=YES;
    search.delegate=self;
    for (UIView *v in search.subviews ) {
        if ([v  isKindOfClass:[UIButton class]]) {
            [(UIButton *)v setTitle:@"关闭"
                           forState:0];
        }
    }
    [search release];
    
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [self.view endEditing:YES];
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    [self.view endEditing:YES];
}
-(void)change:(UIButton *)sender{
    self.tabBarController.tabBar.hidden=YES;
    self.tabBarController.selectedIndex=1;
}

@end

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface BViewController : UIViewController<UITextFieldDelegate>

@end

#import "BViewController.h"

@interface BViewController ()

@end

@implementation BViewController

-(void)loadView{
    [super loadView];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    self.title=@"B";
    //文本视图
    UITextView *textView=[[UITextView alloc]initWithFrame:CGRectMake(0, 0, 320, 400)];
    textView.text=@"jiu  bu gao su ni wo shi shui ";
    textView.scrollEnabled=YES;
    textView.editable=YES;
    textView.backgroundColor=[UIColor grayColor];
    //设置边框
    textView.layer.borderColor=[UIColor blueColor].CGColor;
    //设置边框宽度
    textView.layer.borderWidth=3;
    //设置字体和大小
    textView.font=[UIFont fontWithName:@"Arial" size:14];
    textView.layer.cornerRadius=10;
    //系统字体
    [UIFont familyNames];
    [self.view addSubview:textView];
    [textView release];
    /*
    UIView *hidKeyBordView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
    hidKeyBordView.backgroundColor=[UIColor yellowColor];
    textView.inputAccessoryView=hidKeyBordView;
    [hidKeyBordView release];
    */
    NSMutableArray *array=[NSMutableArray array];
    
    //left
    UIBarButtonItem *oneItem=[[UIBarButtonItem alloc]initWithTitle:@"Done"
                                                             style:UIBarButtonItemStyleBordered
                                                            target:self
                                                            action:@selector(hideKeyBord:)];
    [array addObject:oneItem];
    [oneItem release];
    UIBarButtonItem *spaceItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:
                                //可扩展的
                                UIBarButtonSystemItemFlexibleSpace
                                                            target:nil
                                                            action:nil];
    [array addObject:spaceItem];
    [spaceItem release];

    //right
    UIBarButtonItem *twoItem=[[UIBarButtonItem alloc]initWithTitle:@"Done"
                                                             style:UIBarButtonItemStyleBordered
                                                            target:self
                                                            action:@selector(hideKeyBord:)];
    [array addObject:twoItem];
    [twoItem release];

    
    UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
    toolBar.items=array;
    toolBar.barStyle=UIBarStyleDefault;
    textView.inputAccessoryView=toolBar;
}

-(void)hideKeyBord:(UIBarButtonItem *)item{
    [self.view endEditing:YES];
}
@end

你可能感兴趣的:(textview,UIView)