Ios6 IOS中的按钮操作问题
这是我使用的代码 在视图控制器A中:Ios6 IOS中的按钮操作问题,ios6,Ios6,这是我使用的代码 在视图控制器A中: - (void)viewDidLoad { [super viewDidLoad]; UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button setFrame:CGRectMake(50, 50, 70, 40)]; [button setTitle:@"Next View" forState:UIControlStateNo
- (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。您没有切换视图控制器,而是从视图控制器B获取视图,并将其作为子视图添加到视图控制器A 在这里:
SecondviewController *secondView = [[SecondviewController alloc] init];
[self.view addSubview:secondView.view];
您需要导航到新的视图控制器。。。例如,用这个替换它
SecondviewController *secondViewController = [[SecondviewController alloc] init];
[self presentViewController:secondViewController animated:YES completion:NIL];
(最好在命名控制器时包含“控制器”,以避免与它们的视图混淆)
然后要返回,您需要关闭显示的viewcontroller
在ViewControllerB中,替换以下内容:
[self.view removeFromSuperview];
与
这将从显示的viewController(viewController B)向显示的viewController(viewControllerA)发送一条消息,viewControllerA执行实际的解除操作。您需要在堆栈中显示或推送视图控制器,而不是将第二个子视图添加到第一个子视图中。您只是简单地将其添加为子视图
SecondviewController *secondView = [[SecondviewController alloc] init];
[self presentViewController:secondView animated:NO completion:nil];
在第二个视图控制器中,当您关闭它时,只需从堆栈中关闭/弹出它即可
[self dismissViewControllerAnimated:YES];
发布代码块时,您需要缩进4个空格以正确格式化(已修复)。嗨,AmitApollo..我使用您的代码解决了这个问题。非常感谢。我在UItextview中还有一个查询。我需要在我的应用程序中显示大量的文字作为说明。我在这个案例中使用了UItextview。但是我无法滚动查看全文。。我的代码是:desc=[[UITextView alloc]initWithFrame:CGRectMake(10360303000)];desc.text=@“此处有更多行文字…”;查看UIScrollView。应该有足够的文档帮助您开始使用此控件。
[self dismissViewControllerAnimated:YES];