Iphone開發中navigationItem操作

           使用navigationItem時,首先要確保UINavigationController視圖已經加載到ViewController中。其代碼爲:

                                                           [[UINavigationController alloc] initWithRootViewController:viewController];

1、添加按鈕 

UIBarButtonItem *loginBut =[[UIBarButtonItem alloc] initWithTitle:@"Login" style:UIBarButtonItemStylePlain target:self action:@selector(login)];  

    self.navigationItem.rightBarButtonItem = loginBut;  

    [loginBut release];

  

    UIBarButtonItem *createBut =[[UIBarButtonItem alloc] initWithTitle:@"NEW" style:UIBarButtonItemStylePlain target:self action:@selector(creatUser)];  

    self.navigationItem.leftBarButtonItem = createBut;  

    [createBut release]; 

  

2、添加segmentedControl

 UISegmentedControl *m_segmentedControl = [[UISegmentedControl alloc] initWithItems:

                                            [NSArray arrayWithObjects:

                                            [UIImage imageNamed:@"up.png"],

                                            [UIImage imageNamed:@"down.png"],

                                            nil]];

    [m_segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];

    m_segmentedControl.frame = CGRectMake(0, 0, 90, 30);

    m_segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;

    m_segmentedControl.momentary = YES;

  

    UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:m_segmentedControl];

    [m_segmentedControl release];

  

    self.navigationItem.rightBarButtonItem = segmentBarItem;

    [segmentBarItem release]; 

  

3、設置標題 

 self.navigationItem.title = @"詳細內容"; 

 

4、設置返回按鈕

 self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"主菜單" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章