UISegmentedControl

#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    CGRect rect=[UIScreen mainScreen].bounds;
    self.window=[[UIWindow alloc]initWithFrame:rect];
    self.window.backgroundColor=[UIColor whiteColor];
    ViewController *vc1=[ViewController new];
    UINavigationController *nav=[[UINavigationController alloc]
                                 initWithRootViewController:vc1];
   
    nav.navigationBar.barTintColor=[UIColor cyanColor];//改导航栏的颜色
    //添加阴影
    NSShadow *shadow=[NSShadow new];
    shadow.shadowColor=[UIColor whiteColor];
    shadow.shadowOffset=CGSizeMake(0, 2);
   
   
    NSDictionary *dic=@{NSForegroundColorAttributeName:[UIColor redColor],
                        NSFontAttributeName:[UIFont systemFontOfSize:20],NSShadowAttributeName:shadow};
    //改标题的文字
    nav.navigationBar.titleTextAttributes=dic;
   
   
   
    [self.window setRootViewController:nav];
   
    [self.window makeKeyAndVisible];
   
   
    return YES;
}


#import "ViewController.h"
#import "SecondeViewController.h"
#import "thirstViewController.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
//    UIBarButtonItem *ubi=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(rightclicked)];
   
    UIBarButtonItem *ubi2=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(rightclicked)];
//   
//    UIButton *button1=[UIButton buttonWithType:UIButtonTypeRoundedRect];
//    button1.frame=CGRectMake(300, 20, 20, 20);
//    button1.backgroundColor=[UIColor greenColor];
//    UIBarButtonItem *ubi3=[[UIBarButtonItem alloc]initWithCustomView:button1];
   
    NSArray *arr=@[@"烧鸡",@"青花",@"烧鸡",@"烧鸡"];
    UISegmentedControl *seg=[[UISegmentedControl alloc]initWithItems:arr];
   
    [seg addTarget:self action:@selector(segclick:) forControlEvents:UIControlEventValueChanged];
   //把字体放中间
    self.navigationItem.titleView=seg;
   
//    UIBarButtonItem *ubi4=[[UIBarButtonItem alloc]initWithCustomView:seg];
   
   
    self.navigationItem.leftBarButtonItem=ubi2;
//    self.navigationItem.rightBarButtonItem=ubi4;
 
    self.title=@"第一页";
    self.edgesForExtendedLayout=NO;
    self.view.backgroundColor=[UIColor orangeColor];
    UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame=CGRectMake(0, 20, 100, 40);
    button.backgroundColor=[UIColor greenColor];
    [button setTitle:@"push1" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(pushNext) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
   
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
-(void)segclick:(UISegmentedControl *)seg
{
   NSInteger i = seg.selectedSegmentIndex;
    NSLog(@"%@",[seg titleForSegmentAtIndex:i]);
}


-(void)rightclicked
{
   
}
-(void)pushNext
{
//    UIViewController *vc=self.navigationController.viewControllers[1];
//    //弹出到指定vc
//    [self.navigationController popToViewController:vc animated:YES];
//   
//    [self.navigationController popToRootViewControllerAnimated:YES];
//    //弹出到根vc
    SecondeViewController *vc2=[SecondeViewController new];
    [self.navigationController pushViewController:vc2 animated:YES];
   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


#import "SecondeViewController.h"
#import "thirstViewController.h"
@interface SecondeViewController ()

@end

@implementation SecondeViewController

- (void)viewDidLoad {
    UIBarButtonItem *item=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:nil];
    self.navigationItem.rightBarButtonItem=item;
   
   
    self.title=@"第二页";
    self.view.backgroundColor=[UIColor greenColor];
    UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame=CGRectMake(0, 100, 100, 40);
    button.backgroundColor=[UIColor yellowColor];
    [button setTitle:@"push2" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(push2) forControlEvents:UIControlEventTouchUpInside];
   
    [self.view addSubview:button];
   
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    NSLog(@"%lu",(unsigned long)self.navigationController.viewControllers.count);
}
-(void)push2
{
//    [self.navigationController popToRootViewControllerAnimated:YES];
//    //吐出来
    thirstViewController *vc3=[thirstViewController new];
    [self.navigationController pushViewController:vc3 animated:YES];
    //吞进去
}


#import "thirstViewController.h"

@interface thirstViewController ()

@end

@implementation thirstViewController

- (void)viewDidLoad {
   
    UIBarButtonItem *item=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:nil];
    self.navigationItem.rightBarButtonItem=item;
   
    self.title=@"第三页";
    self.view.backgroundColor=[UIColor whiteColor];
    UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame=CGRectMake(0, 100, 100, 40);
    button.backgroundColor=[UIColor redColor];
    [button setTitle:@"push3" forState:UIControlStateNormal];
    [self.view addSubview:button];
   
    [super viewDidLoad];
    // Do any additional setup after loading the view.

原文地址:https://www.cnblogs.com/linximu/p/4496401.html