iOS UINavigationController

来源:互联网 发布:python windows 界面 编辑:程序博客网 时间:2024/05/29 13:15

//创建导航控制器

    UINavigationController *rootNC = [[UINavigationController alloc] initWithRootViewController:rootVC];

    //给导航条添加颜色

    rootNC.navigationBar.barTintColor = [UIColor redColor];

    //给navigationBar添加图片

    [rootNC.navigationBar setBackgroundImage:[UIImage imageNamed:@"s.jpg"] forBarMetrics:(UIBarMetricsDefault)];

//设置UINavigationBar导航条的按钮

    UIBarButtonItem *but1 = [[UIBarButtonItem alloc] initWithTitle:@"右边" style:(UIBarButtonItemStyleDone) target:self action:@selector(ButtonItemAction:)];

    UIBarButtonItem *but2 = [[UIBarButtonItem alloc] initWithTitle:@"左边" style:(UIBarButtonItemStyleDone) target:self action:@selector(ButtonItemAction:)];

    //添加按钮到NavigationBar右边上面

    self.navigationItem.rightBarButtonItem = but1;

    //添加按钮到NavigationBar左边上面

    self.navigationItem.leftBarButtonItem = but2;

    //给NavigationBar设置标题

    self.navigationItem.title = @"页面1";


//入栈 出栈

//    RootViewController *rt = [[RootViewController alloc] init];

//出栈

//    [self.navigationController popViewControllerAnimated:YES];

    //用数组接收

    NSArray *array = self.navigationController.viewControllers;

    //返回任意页面

    [self.navigationController popToViewController:array[0] animated:YES];

//入栈

//    [self.navigationController pushViewController:rt animated:YES];

    [self.delegate PassView:self.myview.textField.text label:self.myview.label.text];

    //模态

    motaiViewController *mo = [[motaiViewController alloc] init];

    mo.modalTransitionStyle = UIModalPresentationCurrentContext;

    [self  presentViewController:mo animated:YES completion:nil];

    



 页面传值<代理传值> <属性传值>

#import "RootViewController.h"#import "RootView.h"#import "FirstViewController.h"@interface RootViewController ()<PassViewDelegate>@property(nonatomic,strong)RootView *myview;@end@implementation RootViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization        self.myview = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds];        self.view.backgroundColor = [UIColor cyanColor];    }    return self;}-(void)PassView:(NSString*)textFieldString          label:(NSString *)labelString{    self.myview.textField.text = textFieldString;    self.myview.label.text = labelString;}-(void)loadView{    self.view = self.myview;}- (void)viewDidLoad{    [super viewDidLoad];    [self.myview.button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];}-(void)buttonAction:(UIButton *)sender{    FirstViewController *fc = [[FirstViewController alloc] init];    [self.navigationController pushViewController:fc animated:YES];    fc.delegate =self;    fc.textFiledString = self.myview.textField.text;    fc.labelString = self.myview.label.text;        }- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}

#import <UIKit/UIKit.h>@interface RootView : UIView@property(nonatomic,strong)UILabel *label;@property(nonatomic,strong)UITextField *textField;@property(nonatomic,strong)UIButton *button;@end

#import "RootView.h"@implementation RootView- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self)    {        [self addAllViews];    }    return self;}-(void)addAllViews{    self.label = [[UILabel alloc] initWithFrame:CGRectMake(30, 80, 100, 50)];    self.label.backgroundColor = [UIColor whiteColor];    [self addSubview:self.label];    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.label.frame)+20, CGRectGetMinY(self.label.frame), 150, CGRectGetHeight(self.label.frame))];    self.textField.backgroundColor = [UIColor whiteColor];    [self addSubview:self.textField];    self.button = [UIButton buttonWithType:(UIButtonTypeSystem)];    self.button.frame = CGRectMake(100, CGRectGetMaxY(self.label.frame)+30, 100, 100);    self.button.backgroundColor = [UIColor whiteColor];    [self.button setTitle:@"到页面1" forState:(UIControlStateNormal)];    [self addSubview:self.button];    }

#import <UIKit/UIKit.h>#import "PassViewDelegate.h"#import "PassViewDelegate.h"@interface FirstViewController : UIViewController@property(nonatomic,weak)id<PassViewDelegate>delegate;@property(nonatomic,strong)NSString *textFiledString;@property(nonatomic,strong)NSString *labelString;@end

#import "FirstViewController.h"#import "Firstview.h"#import "RootViewController.h"#import "motaiViewController.h"@interface FirstViewController ()@property(nonatomic,strong)Firstview *myview;@end@implementation FirstViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization        self.myview = [[Firstview alloc] initWithFrame:[UIScreen mainScreen].bounds];    }    return self;}-(void)loadView{    self.view = self.myview;}- (void)viewDidLoad{    [super viewDidLoad];    [self.myview.button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];    self.myview.textField.text = self.textFiledString;    self.myview.label.text = self.labelString;    UIBarButtonItem *but1 = [[UIBarButtonItem alloc] initWithTitle:@"左边" style:(UIBarButtonItemStyleDone) target:self action:@selector(but1Action:)];    self.navigationItem.leftBarButtonItem = but1;    UIBarButtonItem *but2 = [[UIBarButtonItem alloc] initWithTitle:@"右边" style:(UIBarButtonItemStyleDone) target:self action:@selector(but1Action:)];    self.navigationItem.rightBarButtonItem = but2;}-(void)but1Action:(UIBarButtonItem *)sender{    RootViewController *rv = [[RootViewController alloc] init];    [self.navigationController pushViewController:rv animated:YES];    }-(void)buttonAction:(UIButton *)sender{//    RootViewController *rt = [[RootViewController alloc] init];//    [self.navigationController popViewControllerAnimated:YES];    //用数组接收    NSArray *array = self.navigationController.viewControllers;    //返回任意页面    [self.navigationController popToViewController:array[0] animated:YES];//    [self.navigationController pushViewController:rt animated:YES];    [self.delegate PassView:self.myview.textField.text label:self.myview.label.text];    //模态    motaiViewController *mo = [[motaiViewController alloc] init];    mo.modalTransitionStyle = UIModalPresentationCurrentContext;    [self  presentViewController:mo animated:YES completion:nil];    }- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}

#import <UIKit/UIKit.h>@interface Firstview : UIView@property(nonatomic,strong)UILabel *label;@property(nonatomic,strong)UITextField *textField;@property(nonatomic,strong)UIButton *button;@end

#import "Firstview.h"@implementation Firstview- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        [self addAllViews];        self.backgroundColor = [UIColor orangeColor];    }    return self;}-(void)addAllViews{    self.label = [[UILabel alloc] initWithFrame:CGRectMake(30, 80, 100, 50)];    self.label.backgroundColor = [UIColor whiteColor];    [self addSubview:self.label];    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.label.frame)+20, CGRectGetMinY(self.label.frame), 150, CGRectGetHeight(self.label.frame))];    self.textField.backgroundColor = [UIColor whiteColor];    [self addSubview:self.textField];    self.button = [UIButton buttonWithType:(UIButtonTypeSystem)];    self.button.frame = CGRectMake(100, CGRectGetMaxY(self.label.frame)+30, 100, 100);    self.button.backgroundColor = [UIColor whiteColor];    [self.button setTitle:@"到页面2" forState:(UIControlStateNormal)];    [self addSubview:self.button];    }

//设置协议
#import <Foundation/Foundation.h>@protocol PassViewDelegate <NSObject>-(void)PassView:(NSString*)textFieldString          label:(NSString *)labelString;@end
#import "AppDelegate.h"#import "RootViewController.h"@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    // Override point for customization after application launch.    RootViewController *rootVC = [[RootViewController alloc] init];    //创建导航条    UINavigationController *rootNC  = [[UINavigationController alloc] initWithRootViewController:rootVC];    rootNC.navigationBar.barTintColor = [UIColor redColor];    self.window.rootViewController = rootNC;    self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];    return YES;}






0 0
原创粉丝点击