2013-02-07 70 views
0

我想用UISegmentedControl在3個視圖之間切換,但我被卡住了。用UISegmentedControl在UIViewControllers之間切換

我用故事板和ARC,我得到這個至今:

我已經拖了分段控制到我的VC,然後我做了一個IBOutlet和IBAction爲連接到分段控制。

這是.h文件看起來像:

#import <UIKit/UIKit.h> 

@interface SCViewController : UIViewController 

@property (strong, nonatomic) IBOutlet UISegmentedControl *segment; 

- (IBAction)changeSeg:(id)sender; 
@end 

然後在.m文件:

#import "SCViewController.h" 

@interface SCViewController() 

@end 

@implementation SCViewController 

@synthesize segment; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) { 
    // Custom initialization 
} 
return self; 
} 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
// Do any additional setup after loading the view. 
} 

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

- (void)viewDidUnload { 
[self setSegment:nil]; 
[super viewDidUnload]; 
} 

- (IBAction)changeSeg:(id)sender { 


if(segment.selectedSegmentIndex == 0){ 
    NSLog(@"page one pressed"); 
    [self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"segmentView1"]]; 

} 
if(segment.selectedSegmentIndex == 1){ 
    NSLog(@"page two pressed"); 
    [self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"segmentView2"]]; 

} 

} 
@end 

該控制檯顯示的NSLog消息,但沒有意見交換(風險投資segmentView1而segmentView2存在於故事板中)

也許我在這方面使用了錯誤的方法?

我想要實現的功能就像iDaft2應用程序。在那裏你有2或3頁,你可以輕鬆地在它們之間切換。

感謝您的幫助!

回答

0

嗯,我已經添加了一個單一的視圖控制器,然後我放入UIViews並使用setHidden按鈕之間切換。我不認爲這是最好的解決方案(應該是更好的解決方案),但它可以完成這項工作。

瀏覽:

// Set views visibility you must make an @property in .h file for each view 
// Like: @property (nonatomic, weak) IBOutlet UIView *firstPage; 
[_firstPage setHidden:NO]; 
[_secondPage setHidden:YES]; 
[_thirdPage setHidden:YES]; 

切換按鈕:

- (IBAction)switchOne:(id)sender { 

[_firstPage setHidden:NO]; 
[_secondPage setHidden:YES]; 
[_thirdPage setHidden:YES]; 
}