xCode - 在 2 个现有的 xib 文件之间更改视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5280104/
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
xCode - Changing views between 2 existing xib-files
提问by Joakim Tennfors
I'm very new to xCode and objective-C so I wanted to make a simple textRPG.
我对 xCode 和 Objective-C 很陌生,所以我想做一个简单的 textRPG。
I'm currently making a character creation process consisting of 4 xib-files. The only way I got switching views to work was to look at the utility-template. Problem is, now I have the first screen being the delegate for the second screen, being the delegate for the third screen etc. So by the end of the character creation process I can't dismiss the views because that just "steps back" through the views.
我目前正在制作一个由 4 个 xib 文件组成的角色创建过程。我切换视图工作的唯一方法是查看实用程序模板。问题是,现在我有第一个屏幕作为第二个屏幕的代表,作为第三个屏幕的代表等等。所以在角色创建过程结束时我不能关闭视图,因为这只是“退后”观点。
When I've searched around for a solution I've found a addSubview-method but it seems like that makes a new view, like, empty to arrange programmatically.
当我四处寻找解决方案时,我发现了一个 addSubview 方法,但似乎这会产生一个新视图,例如,以编程方式排列的空视图。
All I need is a simple way to switch from one loaded xib to another xib. Have I misunderstood addSubview or do I need something completely different?
我所需要的只是一种从一个加载的 xib 切换到另一个 xib 的简单方法。我误解了 addSubview 还是我需要完全不同的东西?
(If it helps: I've worked with VB for several years, in case you notice that I missed some kind of concept concerning views and such) Thanks in advance! :)
(如果有帮助:我已经与 VB 一起工作了几年,以防您注意到我错过了一些关于视图等的概念)提前致谢!:)
回答by ADM
Use this code. It is really simple and works well.
使用此代码。这真的很简单,而且效果很好。
View *view = [[View alloc] initWithNibName:@"xibNameGoesHere" bundle:nil];
view.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:view animated:YES completion:nil];
This will switch to another xib file and the two views won't own one another. I am using it in my own game right now.
这将切换到另一个 xib 文件,两个视图不会相互拥有。我现在在我自己的游戏中使用它。
回答by Marko Hlebar
@Joakim Ok, this is the way I do it. I have a class called RootViewController : UIViewContoller, whose view I add directly to the window. Then I make a subclass of UIView i.e. MyView. All of my views then subclass MyView. I also make an enum for all my views. In RootViewController you should have a switchView:(int)view method that looks something like this:
@Joakim 好的,这就是我的做法。我有一个名为 RootViewController 的类:UIViewContoller,我将其视图直接添加到窗口中。然后我创建了一个 UIView 的子类,即 MyView。然后我所有的视图子类化 MyView。我还为我的所有观点进行了枚举。在 RootViewController 中,你应该有一个 switchView:(int)view 方法,看起来像这样:
-(void) switchView:(myView) view
{
[_currentView removeFromSuperview];
switch(view)
{
case titleView:
_currentView = [[TitleView alloc] initWithRoot:self];
break;
case homeView:
_currentView = [[HomeView alloc] initWithRoot:self];
break;
default: break;
}
[self.view addSubview:_currentView];
[_currentView release];
}
in @interface RootViewContoller define MyView *_currentView; TitleView and HomeView both subclass MyView and have a common method -(id)initWithRoot:(RootViewController*) rvc.
在@interface RootViewContoller 中定义 MyView *_currentView; TitleView 和 HomeView 都是 MyView 的子类,并且有一个共同的方法 -(id)initWithRoot:(RootViewController*) rvc。
To switch between views use [_rvc switchView:homeView]; Hope this helps :)
要在视图之间切换,请使用 [_rvc switchView:homeView]; 希望这可以帮助 :)
回答by hoha
It is called UINavigationController
. Idea is
它被称为UINavigationController
。想法是
1) You push corresponding 'next' controller into navigation controller each time user submits current screen. Bonus - you'll get 'back' button for free on each step of character creation.
1)每次用户提交当前屏幕时,您将相应的“下一个”控制器推入导航控制器。奖励 - 在创建角色的每个步骤中,您都会免费获得“返回”按钮。
2) After character is created you pop all character creation controllers from stack.
2)创建角色后,您从堆栈中弹出所有角色创建控制器。
Please read View Controller Programming Guide for iOSbefore trying to 'switch views' and such. You'll save tons of time and nerves.
在尝试“切换视图”等之前,请阅读iOS 的视图控制器编程指南。您将节省大量时间和精力。
回答by Marko Hlebar
another idea is not to use interface builder at all. i have been working with iphone apps for two years now and found that interface builder really prolongs the time to actually make something. make your own root controller and think about the logic you need to navigate through the views.
另一个想法是根本不使用界面构建器。我已经使用 iphone 应用程序工作了两年,发现界面构建器确实延长了实际制作某些东西的时间。制作自己的根控制器并考虑浏览视图所需的逻辑。