2009-06-26 38 views

回答

109

最簡單的方法是有兩個視圖,您可以切換其可見性以指示已選擇哪個視圖。下面是它如何做,絕對不是一個優化的方式來處理的意見,但只是爲了演示如何使用UISegmentControl來切換可見視圖一些示例代碼:

- (IBAction)segmentSwitch:(id)sender { 
    UISegmentedControl *segmentedControl = (UISegmentedControl *) sender; 
    NSInteger selectedSegment = segmentedControl.selectedSegmentIndex; 

    if (selectedSegment == 0) { 
    //toggle the correct view to be visible 
    [firstView setHidden:NO]; 
    [secondView setHidden:YES]; 
    } 
    else{ 
    //toggle the correct view to be visible 
    [firstView setHidden:YES]; 
    [secondView setHidden:NO]; 
    } 
} 


您當然可以進一步重新編碼以隱藏/顯示正確的視圖。

+4

「絕對不是一種優化的方式來處理的意見' - 爲什麼? – 2012-11-22 21:52:54

+3

@AdamWaite因爲所有視圖都必須永久存儲在內存中。如果您的觀點太複雜和/或包含許多其他元素,將會影響整體表現。這段代碼也可以重構。 – Stas 2013-07-02 05:23:45

+0

@Stas你是對的,最好將多個視圖控制器之間的邏輯分開,每個負責他自己的行爲和行爲 – 2015-02-18 21:09:00

17

或者,如果它的表,您可以重新加載表,並在cellForRowAtIndex,根據選定的段選項從不同的數據源填充表。

7

一個想法是讓分段控件的視圖有一個容器視圖,您可以填充不同的子視圖(在切換切片時添加爲容器視圖的唯一子視圖)。你甚至可以爲這些子視圖設置單獨的視圖控制器,但是如果你需要它們(並且必須知道它們在哪個導航控制器下),你必須轉發諸如「viewWillAppear」和「viewWillDisappear」之類的重要方法。

通常情況下,因爲您可以使用容器在IB中佈局主視圖,並且子視圖將填充容器允許其擁有的任何空間(確保您的autoresize掩碼設置正確)。

45

就我而言,我的觀點非常複雜,我不能只是改變不同視圖的隱藏屬性,因爲它會佔用太多的內存。

我已經嘗試了幾種解決方案,並且它們都不適合我,或者執行不正常,特別是navBar的titleView在推/彈出視圖時並不總是顯示segmentedControl。

我發現這篇博客文章解釋瞭如何以正確的方式做到這一點。似乎他得到了WWDC'2010的Apple工程師的幫助,想出了這個解決方案。

http://redartisan.com/2010/6/27/uisegmented-control-view-switching-revisited

在這個環節的解決方案是手了最好的解決方案,我迄今發現有關問題。隨着調整的一點點,也工作得很好,在底部

2

分配.H在

UISegmentedControl *lblSegChange; 

- (IBAction)segValChange:(UISegmentedControl *) sender 

申報。中號

- (IBAction)segValChange:(UISegmentedControl *) sender 
{ 

if(sender.selectedSegmentIndex==0) 
{ 
    viewcontroller1 *View=[[viewcontroller alloc]init]; 
    [self.navigationController pushViewController:view animated:YES]; 
} 
else 
{ 
    viewcontroller2 *View2=[[viewcontroller2 alloc]init]; 
    [self.navigationController pushViewController:view2 animated:YES]; 
} 
} 
2

從@Ronnie劉氏的答案,我創建這個:

// 
// ViewController.m 
// ResearchSegmentedView 
// 
// Created by Ta Quoc Viet on 5/1/14. 
// Copyright (c) 2014 Ta Quoc Viet. All rights reserved. 
// 
#define SIZE_OF_SEGMENT 56 
#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 
@synthesize theSegmentControl; 
UIView *firstView; 
UIView *secondView; 
CGRect leftRect; 
CGRect centerRect; 
CGRect rightRect; 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    leftRect = CGRectMake(-self.view.frame.size.width, SIZE_OF_SEGMENT, self.view.frame.size.width, self.view.frame.size.height-SIZE_OF_SEGMENT); 
    centerRect = CGRectMake(0, SIZE_OF_SEGMENT, self.view.frame.size.width, self.view.frame.size.height-SIZE_OF_SEGMENT); 
    rightRect = CGRectMake(self.view.frame.size.width, SIZE_OF_SEGMENT, self.view.frame.size.width, self.view.frame.size.height-SIZE_OF_SEGMENT); 

    firstView = [[UIView alloc] initWithFrame:centerRect]; 
    [firstView setBackgroundColor:[UIColor orangeColor]]; 
    secondView = [[UIView alloc] initWithFrame:rightRect]; 
    [secondView setBackgroundColor:[UIColor greenColor]]; 
    [self.view addSubview:firstView]; 
    [self.view addSubview:secondView]; 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (IBAction)segmentSwitch:(UISegmentedControl*)sender { 
    NSInteger selectedSegment = sender.selectedSegmentIndex; 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.2]; 
    if (selectedSegment == 0) { 
     //toggle the correct view to be visible 
     firstView.frame = centerRect; 
     secondView.frame = rightRect; 
    } 
    else{ 
     //toggle the correct view to be visible 
     firstView.frame = leftRect; 
     secondView.frame = centerRect; 
    } 
    [UIView commitAnimations]; 
} 
@end 
1

快速斯威夫特版本:

@IBAction func segmentControlValueChanged(_ sender: UISegmentedControl) { 

    if segmentControl.selectedSegmentIndex == 0 { 

     // do something 
    } else { 

     // do something else 
    } 
}