IOS學習筆記(5)

來源:互聯網
上載者:User

上一篇:http://www.bkjia.com/kf/201301/185018.html
UINavigationController實現導航在App委託中的.h檔案裡@property( nonatomic ,strong)UINavigationController *nav;.m檔案裡@synthesize nav;-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{     self.nav = [ [UINavigationController alloc]initWithRootViewController:self.rootViewController];    [self.window addSubview:  self.nav.view ];    return YES;}在viewController頁面裡面-(void)viewDidLoad{   self.title = @"hello World";}更深一點的:想在第一個視圖控制器出現在螢幕上5秒後把第二個視圖控制器拖到它的頂部:首先在第一個視圖控制器放入第二個視圖控制器:#import "SecondViewController"-(void)pushSecondController{    SecondViewController *secondController = [ [ SecondViewController alloc]initWithNibName:nil bundle:NULL];    [ self.nav pushViewController:secondController animated:YES];}-(void)viewDidAppear:(BOOL)paramAnimated{    [super viewDidAppear:paramAnimated];    [self performSelector:@selecter(pushSecondController) withObject:nil afterDelay:5.0f];    }既然能夠拖進來,那麼就能移出去:-(void)goBack{    [ self.nav  popViewControllerAnimated:YES];}-(void)DidAppear:(BOOL)paramAnimated{    [super viewDidAppear:paramAnimated];    [self performSelector:@selector(goBack) withObject:nil afterDelay:5.0f];}調整視圖控制器中導航控制器的序列使用 UINavigationController 類的 viewControllers 屬性獲得並修改與導航控制器關聯的視圖控制器的排列順 序:  -(void)goBack{    NSArray *currentControllers =  self.nav.viewControllers;    NSMutableArray *newControllers = [NSMutableArray arrayWithArray:currentControllers];    [newControllers removeLastObject];     self.nav.viewControllers = newControllers;    //動畫完成    //[self.nav setViewControllers:newControllers animated:YES];}為了從當前視圖控制器相關聯的導航控制器的階層中推送最後一個視圖控制器,可以在任何視圖控制 器內調用此方法。 在導覽列展示一張圖片要在導航控制器的當前視圖中的標題中用一張圖片代替文本-(void)viewDidLoad{    [super viewDidLoad];    self.view.backgroundColor = [UIColor whiteColor];    UIImageView *imageView = [ [UIImageView alloc] initWithFrame:CGRectMake(0.0f,0.0f,100.0f,40.0f);    imageView.contentMode = UIViewContentModeScaleAspectFit;    UIImage *image = [UIImage imageNamed:@"FullSizeLogo.png"];    [imageView setImage:image];    self.navigationItem.titleView = imageView;}使用UIBarButtonItem類在導覽列裡面添加按鈕建立一個導覽按鈕前提:建立一個UIBarButtonItem類執行個體,使用視圖空間的NavigationItem屬性給導覽列添加按鈕。NavigationItem屬性允許我們設定這個導覽列。這個屬性自身有兩個屬性,分別為rightBarButtonItem 和 leftBarButtonItem 。這兩個屬性都屬於UIBarButtonItem類。-(void)viewDidLoad{    [super viewDidLoad];    self.view.backgroundColor = [UIColor whiteColor];    self.title = @"hello World";    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"right" style:UIBarButtonItemStylePlain target:self action:@selector(performRight:)];}-(void) performRight:(id)right{    NSLog(@"clicked rightButton");}系統按鈕初始化方法一:initWithBarButtonSystemItem:target:action:初始化方法self.navigationItem.leftBarButtonItem = [ [UIBarButtonItem alloc] initWithBartButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(performRight:)];導覽按鈕的初始化按鈕:typedef enum{    UIBarButtonSystemItem Done/Cancel/Edit/Save/Add/FlexibleSpace/FixedSpace/Compose/Reply/Action/Organize/Bookmarks/Search/Refresh/Stop/Camera/Trash/Play/Pause/Rewind/FastForward/Undo/Redo/PageCurl/}UIBarButtonSystemItem; 系統按鈕初始化方法二:initWithCustomView:方法(可以將UISwitch添加)-(void)viewDidLoad{    [super viewDidLoad];    self.view.backgroundColor = [UIColor whiteColor];    self.title = @"hello World";    UISwitch *mySwitch = [ [UISwitch alloc]init];    mySwitch.on = YES;    [mySwitch addTarget:self action:@selector(SwitchChanged:) forControlEvents:UIControlEventValueChanged];    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:mySwitch];}可以了,測試一下吧。那麼再做一個上下箭頭的demo吧。-(void)viewDidLoad{    [super viewDidLoad];    self.view.backgroundColor = [UIColor whiteColor];    self.title = @"hello World";    NSArray *items = [[NSArray alloc]initWithObjects:[UIImage imageNamed:@"UpArrow.png"],[UIImage imageNamed:@"DownArrow.png"],nil];    UISegmentedControl *segmentedControl = [ [UISegmentedControl alloc]initWithItems:items];    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;    segmentedControl.momentary = YES;    [segmentedControl addTarget:self action:@selector(segmentedControlTapped:) forControlEvents:UIControlEventValueChanged];    self.navigationItem.rightBarButtonItem = [ [UIBarButtonItem alloc]initWithCustomView:segmentedControl];    //設定動畫    //UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];    [self.navigationItem setRightBarButtonItem:rightBarButton animated:YES];}使用UITabBarController顯示多視圖控制器AppDelegate.h裡面@class FirstViewController;@class SecondViewController; @property( nonatomic,strong)FirstViewController *firstViewController;@property(non atomic,strong)SecondViewController *secondViewController;@property(non atomic,strong)UITableBarController *tabBarController;AppDelegate.m檔案裡@synthesize firstViewController,secondViewController,tabBarController; self.firstViewController = [[FirstViewController alloc]initWithNibName:nil bundle:NULL];self.secondViewController = [[SecondViewController alloc]initWithNibName:nil bundle:NULL];NSArray *twoViewControllers = [ [NSArray alloc]initWithObjects:self.firstViewController,self.secondViewController,nil];self.tabBarController = [[UITabBarController alloc]init];[self.tabBarController setViewControllers:twoViewControllers]; 運行程式一看,沒有導航啊。怎麼辦呢?接著往下進行吧。在AppDelegate.h中@proterty(non atomic,strong)UINavigationController *nav;在AppDelegate.m檔案裡@synthesize nav;self.firstNavigationController = [[UINavigationController alloc]initWithRootViewController:self.firstViewController];self.secondNavigationController = [[UINavigationController alloc]initWithRootViewController:self.secondViewController];NSArray *twoNavigationController = [[NSArray alloc]initWithObjects:self.firstNavigationController,self.secondNavigationController,nil];self.tabBarController = [[UITabBarController alloc]init];[self.tabBarController setViewControllers:twoNavigationController];self.window addSubview:self.tabBarController.view];tabbarItem屬性:firstViewController.m-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if(self!=nil){        self.title = @"First";        self.tabBarItem.image = [UIImage image named:@"FirstTb.png"];    }    return self;}secondViewController 同上 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.