ios 在按钮单击时更改 viewController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5814752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:44:10  来源:igfitidea点击:

Changing viewController on Button Click

ioscocoa-touchuibuttonviewcontroller

提问by Vik

I am new to iPhone programming. what I am trying is I have one screen with a button. And I want to change the view controller not only the view when I click that button (I know how to add subview) because from that 2nd view controler, I have to go to the third view which is not possible if I add subview in first place. Can anybody please help me with that? Is this possible? and if yes, how? All the views and view controller are created programmaticaly. I am not using IB.

我是 iPhone 编程的新手。我正在尝试的是我有一个带按钮的屏幕。我想更改视图控制器,不仅是单击该按钮时的视图(我知道如何添加子视图),因为从第二个视图控制器开始,我必须转到第三个视图,如果我首先添加子视图,这是不可能的地方。有人可以帮我吗?这可能吗?如果是,如何?所有视图和视图控制器都是以编程方式创建的。我没有使用IB。

EDIT: here is the relevant code that fires when clicking button

编辑:这是单击按钮时触发的相关代码

-(id)showCurrentLoc:(id)sender { 
 locationController = [currentLocController alloc]; 
 [entry removeFromSuperview]; 
 [newLoc removeFromSuperview]; 
 [currentLoc removeFromSuperview]; 
 [self.view setBackgroundColor:[UIColor clearColor]]; //[self.view addSubview: [locationController view]]; 
 [self.navigationController pushViewController:locationController animated:YES];  [locationController release]; 
 return 0; 
} //Location Controller is the tableViewController

Thanks Vik

谢谢维克

回答by Janak Nirmal

You can do something like this

你可以做这样的事情

        YourViewController *objYourViewController = [[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil];
        [self.navigationController pushViewController:objYourViewController animated:YES];
        [YourViewController release];

回答by Caleb

Usually, you use a navigation controller for this sort of thing so that the user can easily go back to the previous view. Your view controller would then do something like this:

通常,您会使用导航控制器进行此类操作,以便用户可以轻松返回到之前的视图。然后你的视图控制器会做这样的事情:

[self.navigationController pushViewController:someNewViewController animated:YES];

If you want to manage the view controllers yourself, you can always just change the window's rootViewControllerproperty. Please read View Controller Programming Guidefor the complete picture.

如果您想自己管理视图控制器,您可以随时更改窗口的rootViewController属性。请阅读视图控制器编程指南以获取完整图片。

回答by Krishnabhadra

UINavigationControlleris what you need. It manages a stack of UIViewControllers and if you want to add new UIViewController just push it into this navigation stack. It automates back button behavior for you, and you can pop your current UIViewController from stack whenever you are done with it.

UINavigationController正是您所需要的。它管理一个UIViewControllers堆栈,如果你想添加新的 UIViewController 只需将它推入这个导航堆栈。它为您自动执行后退按钮行为,您可以在完成后从堆栈中弹出当前的 UIViewController。

回答by ophychius

You could work with a UINavigationController. Adding your first UIViewController like this in the init method:

您可以使用 UINavigationController。在 init 方法中像这样添加你的第一个 UIViewController:

        [self setViewControllers:[NSArray arrayWithObject:viewController]];

And then when a button is click or a choice is made, your push the second few controller with (in the first viewController):

然后当一个按钮被点击或做出选择时,你用(在第一个 viewController 中)推动第二个控制器:

[self.navigationController pushViewController:controller animated:YES]; 

This way you will also get an automatic (back button). Basicly you create a stack of UIViewControllers that you can push and pop like with a normal stack.

这样,您还将获得一个自动(后退按钮)。基本上,您创建了一个 UIViewControllers 堆栈,您可以像普通堆栈一样推送和弹出它们。

I hope this helps. Look into the following: UINavigationController Class Reference

我希望这有帮助。查看以下内容: UINavigationController 类参考

回答by Chetan Bhalara

- (void) loadViewAtIndex:(NSInteger)index {

    [self unloadViewAtIndex:activeViewIndex];

    switch (index) {

        case 1:
        {
            if (viewController1 == nil)
            {
                viewController1 = [[ViewController1 alloc] initWithNibName:@"ViewController1" bundle:nil];
            }

            viewController1.view.frame = CGRectMake(0.0, 0.0, viewController1.view.frame.size.width, viewController1.view.frame.size.height);

            [window addSubview:viewController1.view];
        }
            break;

        case 2:
        {
            if (viewController2 == nil)
            {
                viewController2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];
            }

            viewController2.view.frame = CGRectMake(0.0, 0.0, viewController2.view.frame.size.width, viewController2.view.frame.size.height);

            [window addSubview:viewController2.view];
        }
            break;

        default:
            break;
    }

    activeViewIndex = index;
}

- (void) unloadViewAtIndex:(NSInteger)index {
    switch (index) {

        case 1:
        {
            if (viewController1 != nil)
            {
                [viewController1.view removeFromSuperview];
                [viewController1 release];
                viewController1 = nil;
            }
        }
            break;

        case 2:
        {
            if (viewController2 != nil)
            {
                [viewController2.view removeFromSuperview];
                [viewController2 release];
                viewController2 = nil;
            }
        }
            break;

        default:
            break;
    }
}