在 xcode 中的故事板中添加和控制视图控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12119451/
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
Adding and controlling view controllers in storyboard in xcode
提问by Muhammad Umar
I am new to iphone programming and I have created a special scenario. I am pretty sure that it will answer most of my questions for creating my app. Here it is. (i HAVE Xcode 4.2
)
我是 iphone 编程的新手,我创建了一个特殊的场景。我很确定它会回答我创建应用程序的大部分问题。这里是。(我有Xcode 4.2
)
I have created a universal application with storyboard and single view.
我创建了一个带有故事板和单一视图的通用应用程序。
I have appDelegate
, storyboard files
and a class file ViewController.h/m
for my initial ViewController
.
我有appDelegate
,storyboard files
和ViewController.h/m
我的初始ViewController
.
Suppose I have added a progress View
(Graphically) on the view Controller. Also I have added another viewController
on storyboard and made its background black.
假设我progress View
在视图控制器上添加了一个(以图形方式)。我viewController
还在故事板上添加了另一个并将其背景设为黑色。
When i run the app my first viewController
with progress view shows up
当我运行应用程序时,我的第一个viewController
进度视图出现了
Now my questions are
现在我的问题是
1- How can I link my progress view in the class file and how can i set it progress for 5 seconds.
1- 如何在类文件中链接我的进度视图,以及如何将进度设置为 5 秒。
2- After showing progress within 5 seconds it should switch to other view Controller with black background.
2- 在 5 秒内显示进度后,它应该切换到其他黑色背景的视图控制器。
3- I have created a class file "MySecondController
" How can i link this class to my black screen viewController.
3- 我创建了一个类文件“ MySecondController
” 我如何将这个类链接到我的黑屏 viewController。
These are easy questions. I hope I get answer to these. If anyone have tutorials linking to these questions do post. I have tried and haven't found useful.
这些都是简单的问题。我希望我能得到这些答案。如果有人有链接到这些问题的教程,请发布。我试过了,没有发现有用。
Best Regards
此致
回答by danh
1a) On the upper right of the xcode window, there's a tuxedo icon. This is the assistant editor. Select the storyboard, select the ViewController, then select the assistant editor. A second editor will appear. Make sure it's editing ViewController.h. Press control and drag from the progress view in the storyboard to a point inside the ViewController interface definition. (right after the line that looks like this: @interface ViewController : UIViewController
) You'll be prompted to type an outlet name. Type something like "progressView" (no quotes).
1a) 在 xcode 窗口的右上角,有一个燕尾服图标。这是助理编辑。选择storyboard,选择ViewController,然后选择助手编辑器。将出现第二个编辑器。确保它正在编辑 ViewController.h。按下控制并从故事板中的进度视图拖动到 ViewController 接口定义内的某个点。(就在看起来像这样的行之后:)系统@interface ViewController : UIViewController
会提示您输入插座名称。键入类似“progressView”(无引号)的内容。
1b) See below to set the progress view to a value.
1b) 请参阅下文以将进度视图设置为一个值。
2) See below to present the second view controller after a delay.
2) 请参阅下文以在延迟后呈现第二个视图控制器。
3) In the storyboard, drag a new viewcontroller onto the canvas. On the identity inspector (try the icons under the tuxedo icon, the third from the left is identity), set it's class to MySecondController. Select the original ViewController, press control and drag from it's status bar (the top of it) to the viewcontroller you just added. This will create a segue. A menu will appear asking what kind of segue you want. Make the segue modal for now. Click on the new segue to select it, then click on the attributes inspector (right next to identity inspector) give the segue an identifier, call it "MySecondControllerSegue".
3) 在情节提要中,将一个新的视图控制器拖到画布上。在身份检查器上(试试燕尾服图标下的图标,左起第三个是身份),将它的类设置为 MySecondController。选择原始的 ViewController,按下 control 并从它的状态栏(它的顶部)拖动到您刚刚添加的 viewcontroller。这将创建一个segue。将出现一个菜单,询问您想要什么样的转场。现在制作 segue 模态。单击新的 segue 以选择它,然后单击属性检查器(在身份检查器旁边)为 segue 提供一个标识符,将其命名为“MySecondControllerSegue”。
Back to questions 1b and 2) Go to ViewController.m and add this code...
回到问题 1b 和 2) 转到 ViewController.m 并添加此代码...
@synthesize progressView;
// this creates methods on self to access the progress view
// this is called after the view appears
- (void)viewDidAppear:(BOOL)animated {
// do inherited behavior
[super viewDidAppear:animated];
// set the progress view value to a number between 0.0-1.0, representing percentage
// notice how we used the synthesized getter: self.progressView
self.progressView.progress = 0.5;
// call another method with no parameters after five seconds
[self performSelector:@selector(doSegue) withObject:nil afterDelay:5.0];
}
- (void)doSegue {
// run the segue that you created in IB
[self performSegueWithIdentifier:@"MySecondControllerSegue" sender:self];
}
Build and run.
构建并运行。
After this is working, you'll probably want to show that progress view advancing from the 0.0 position to 1.0 over the course of five seconds. For that, you'll need to read about NSTimer. You can set one up to send you a message periodically, and you can adjust the progressView each time.
完成此操作后,您可能希望显示在 5 秒内从 0.0 位置前进到 1.0 的进度视图。为此,您需要阅读 NSTimer。您可以设置一个定期给您发送消息,并且您可以每次调整progressView。