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頁,你可以輕鬆地在它們之間切換。
感謝您的幫助!