我有一个通用的UIViewController所有的uiviewscontroller都扩展了它来重用一些通用的操作。
我想在这个Common UIViewController上建立一个segue这样所有其他的UIViewController都会继承。
我想知道如何通过编程来实现。
我想这个问题也可能是我如何为我所有的uiviewcontroller设置segue而不需要进入故事板手工操作。
我有一个通用的UIViewController所有的uiviewscontroller都扩展了它来重用一些通用的操作。
我想在这个Common UIViewController上建立一个segue这样所有其他的UIViewController都会继承。
我想知道如何通过编程来实现。
我想这个问题也可能是我如何为我所有的uiviewcontroller设置segue而不需要进入故事板手工操作。
当前回答
我对UIStoryboard的segue进行了逆向工程,并做了一个开源(重新)实现:https://github.com/acoomans/Segway
有了这个库,就可以编程地定义segue(不需要任何故事板)。
希望能有所帮助。
其他回答
你可以创建UIStoryBoardSegue的子类。子类化主要用于提供自定义过渡动画。
你可以看到wwdc 2011介绍StoryBoard的视频。YouTube上也有。
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIStoryboardSegue_Class/Reference/Reference.html#//apple_ref/occ/cl/UIStoryboardSegue
我一直在使用这段代码来实例化我的自定义segue子类并以编程方式运行它。这似乎很有效。这有什么问题吗?我很困惑,看到所有其他的答案都说这是不可能的。
UIViewController *toViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"OtherViewControllerId"];
MyCustomSegue *segue = [[MyCustomSegue alloc] initWithIdentifier:@"" source:self destination:toViewController];
[self prepareForSegue:segue sender:sender];
[segue perform];
首先,假设你在故事板中有两个不同的视图,你想从一个屏幕导航到另一个屏幕,那么按照以下步骤:
1).在身份检查器中使用类文件和故事板id定义所有视图。
2).确保你添加了一个导航控制器到第一个视图。在故事板中选择它,然后编辑器>嵌入>导航控制器
3).在你的第一个类中,导入"secondClass.h"
#import "ViewController.h
#import "secondController.h"
4).在必须执行segue的IBAction中添加此命令
secondController *next=[self.storyboard instantiateViewControllerWithIdentifier:@"second"];
[self.navigationController pushViewController:next animated:YES];
5). @"second"是secondview控制器类,故事板id。
你必须将你的代码链接到你正在使用的UIStoryboard。确保你进入UIStoryboard中的YourViewController,点击它周围的边框,然后设置它的标识符字段为你在代码中调用的NSString。
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
bundle:nil];
YourViewController *yourViewController =
(YourViewController *)
[storyboard instantiateViewControllerWithIdentifier:@"yourViewControllerID"];
[self.navigationController pushViewController:yourViewController animated:YES];
我想我可以加上另一种可能性。你能做的一件事是你能用一个没有附加到动作的segue连接故事板中的两个场景,然后在你的视图控制器中以编程方式触发这个segue。你这样做的方法是,你必须从故事板场景底部的文件所有者图标拖拽到segue场景,然后右拖拽到目标场景。我将附上一张图片来帮助解释。
弹出窗口将显示“手动Segue”。我选择Push作为类型。点击小方块,确保你在属性检查器中。给它一个标识符,用于在代码中引用它。
接下来我要用一个程序化的栏按钮项进行segue。在viewDidLoad或其他地方,我将用下面的代码在导航栏上创建一个按钮项:
UIBarButtonItem *buttonizeButton = [[UIBarButtonItem alloc] initWithTitle:@"Buttonize"
style:UIBarButtonItemStyleDone
target:self
action:@selector(buttonizeButtonTap:)];
self.navigationItem.rightBarButtonItems = @[buttonizeButton];
注意到选择器是buttonizeButtonTap:。所以为那个按钮写一个void方法在这个方法中你会像这样调用segue:
-(void)buttonizeButtonTap:(id)sender{
[self performSegueWithIdentifier:@"Associate" sender:sender];
}
当调用prepareForSegue时,需要sender参数来标识按钮。prepareForSegue是框架方法,你将实例化你的场景,并传递给它任何值,它将需要做它的工作。下面是我的方法:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"Associate"])
{
TranslationQuizAssociateVC *translationQuizAssociateVC = [segue destinationViewController];
translationQuizAssociateVC.nodeID = self.nodeID; //--pass nodeID from ViewNodeViewController
translationQuizAssociateVC.contentID = self.contentID;
translationQuizAssociateVC.index = self.index;
translationQuizAssociateVC.content = self.content;
}
}
我测试过了,效果很好。