這是我用過的代碼。IOS中的按鈕操作問題
在View控制器A:
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(50, 50, 70, 40)];
[button setTitle:@"Next View" forState:UIControlStateNormal];
[button addTarget:self action:@selector(nextView) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void) nextView
{
SecondviewController *secondView = [[SecondviewController alloc] init];
[self.view addSubview:secondView.view];
}
在視圖控制器B:
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(50, 50, 70, 40)];
[button setTitle:@"Previous View" forState:UIControlStateNormal];
[button addTarget:self action:@selector(previousView) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void) previousView
{
[self.view removeFromSuperview];
}
問題:當我在視圖控制器B點擊按鈕,它不是切換回視圖控制器A ...
發佈代碼塊時,您需要縮進4個空格才能正確格式化(固定)。 – foundry 2013-03-20 14:15:38