模态与非模态进入和退出UIViewController以及UINavigationController简单用法

UIViewController有两个常用属性,一个控制导航显示内容,一个是自身导航控制器

@property(nonatomic,readonly,retain)UINavigationItem *navigationItem;// Created on-demand so that a view controller may customize its navigation appearance.

@property(nonatomic,readonly,retain)UINavigationController *navigationController;// If this view controller has been pushed onto a navigation controller, return it.

UINavigationItem的常用属性

@property(nonatomic,copy)  NSString        *title;            // Title when topmost on the stack. default is nil   导航视图控制器标题

@property(nonatomic,retain)UIBarButtonItem *leftBarButtonItem;  // 左导航键

@property(nonatomic,retain)UIBarButtonItem *rightBarButtonItem; //右导航键

UIBarButtonItem常用构造方法

- (id)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;


UINavigationController继承自UIViewController

一般用法是创建一个UINavigationController将UIViewController设置为它的根视图来显示

常用属性

@property(nonatomic,readonly)UINavigationBar *navigationBar;// The navigation bar managed by the controller. Pushing, popping or setting navigation items on a managed navigation bar is not supported.  

// 常常将它的属性 translucent 设置为NO来控制导航栏不透明,此时座标原点移动到导航栏下方视图的左上角

UINavigationBar 常用属性

@property(nonatomic,assign,getter=isTranslucent)BOOL translucent NS_AVAILABLE_IOS(3_0);// Default is NO on iOS 6 and earlier. Always YES if barStyle is set to UIBarStyleBlackTranslucent 

示例

AppDelegate.m中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]]autorelease];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColorwhiteColor];

    FirstViewController * firstViewController = [[FirstViewControlleralloc]init];

    UINavigationController * nav = [[UINavigationControlleralloc]initWithRootViewController:firstViewController];

    [self.windowsetRootViewController:nav];

    [self.windowmakeKeyAndVisible];

    return YES;

}


FirstViewController.m中

- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view.

    

    //设置导航标题和导航右键

    [self.navigationItemsetTitle:@"第一视图"];

    [self.navigationController.navigationBarsetTranslucent:NO];//设置导航栏不透明

    UIBarButtonItem * rightBarButtonItem = [[UIBarButtonItemalloc]initWithTitle:@"next"style:UIBarButtonItemStyleDonetarget:selfaction:@selector(btnAction)];

    [self.navigationItemsetRightBarButtonItem:rightBarButtonItem];

    [rightBarButtonItem release];

    //测试按钮

    UIButton * btn = [[UIButtonalloc]initWithFrame:CGRectMake(60,200,200,50)];

    [btn setBackgroundColor:[UIColorgrayColor]];

    [btn setTitle:@"进入下一视图控制器" forState:UIControlStateNormal];

    [btn addTarget:selfaction:@selector(btnAction)forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    [btn release];

}


-(void)btnAction

{

    SecondViewController * secondViewController = [[SecondViewControlleralloc]init];

    

    //模态进入

    //UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:secondViewController];

    //[self presentViewController:nav animated:NO completion:^{}];

    //[secondViewController release];

    //[nav release];

    

    //非模态进入

    [self.navigationControllerpushViewController:secondViewControlleranimated:NO];

    [secondViewController release];

    

    

}


SecondViewController中

- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view.

    

    //设置导航标题和导航左键

    [self.navigationItemsetTitle:@"第二视图"];

    UIBarButtonItem * leftBarButtonItem = [[UIBarButtonItemalloc]initWithTitle:@"previous"style:UIBarButtonItemStyleDonetarget:selfaction:@selector(btnAction)];

    [self.navigationItemsetLeftBarButtonItem:leftBarButtonItem];

    [leftBarButtonItem release];

    //测试按钮事件

    UIButton * btn = [[UIButtonalloc]initWithFrame:CGRectMake(60,200,200,50)];

    [btn setBackgroundColor:[UIColorgreenColor]];

    [btn setTitle:@"返回上一视图控制器" forState:UIControlStateNormal];

    [btn addTarget:selfaction:@selector(btnAction)forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    [btn release];


}

-(void)btnAction

{

    //模态返回

    //[self dismissViewControllerAnimated:NO completion:^{}];

    

    //非模态返回

    [self.navigationControllerpopViewControllerAnimated:NO];

 }















发布了11 篇原创文章 · 获赞 3 · 访问量 2万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章