我有一个通用的UIViewController所有的uiviewscontroller都扩展了它来重用一些通用的操作。
我想在这个Common UIViewController上建立一个segue这样所有其他的UIViewController都会继承。
我想知道如何通过编程来实现。
我想这个问题也可能是我如何为我所有的uiviewcontroller设置segue而不需要进入故事板手工操作。
我有一个通用的UIViewController所有的uiviewscontroller都扩展了它来重用一些通用的操作。
我想在这个Common UIViewController上建立一个segue这样所有其他的UIViewController都会继承。
我想知道如何通过编程来实现。
我想这个问题也可能是我如何为我所有的uiviewcontroller设置segue而不需要进入故事板手工操作。
当前回答
根据定义,segue不能独立于故事板而存在。它甚至存在于类名中:UIStoryboardSegue。你不能通过编程方式创建segue——是故事板运行时为你创建它们。你通常可以在你的视图控制器代码中调用performSegueWithIdentifier:,但这依赖于在故事板中已经设置了一个segue来引用。
我认为你问的是如何在你的公共视图控制器(基类)中创建一个方法,它将转换到一个新的视图控制器,并将被所有派生类继承。你可以为你的基类视图控制器创建一个这样的方法:
- (IBAction)pushMyNewViewController
{
MyNewViewController *myNewVC = [[MyNewViewController alloc] init];
// do any setup you need for myNewVC
[self presentModalViewController:myNewVC animated:YES];
}
然后在你的派生类中,当适当的按钮被点击或表行被选中时调用那个方法。
其他回答
实际上有几个问题:
首先,在你为我们上传的那个项目中,segue没有“segue1”标识符:
没有标识符
如果您还没有填写标识符,您应该填写。
第二,当你从表视图推到表视图时,你在调用initWithNibName来创建一个视图控制器。你需要使用instantiateViewControllerWithIdentifier。
我想我可以加上另一种可能性。你能做的一件事是你能用一个没有附加到动作的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;
}
}
我测试过了,效果很好。
我一直在使用这段代码来实例化我的自定义segue子类并以编程方式运行它。这似乎很有效。这有什么问题吗?我很困惑,看到所有其他的答案都说这是不可能的。
UIViewController *toViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"OtherViewControllerId"];
MyCustomSegue *segue = [[MyCustomSegue alloc] initWithIdentifier:@"" source:self destination:toViewController];
[self prepareForSegue:segue sender:sender];
[segue perform];
故事板segue不能在故事板之外创建。尽管有一些缺点,但您需要将其连接起来。
UIStoryboardSegue Reference明确指出:
你不能直接创建segue对象。相反,是故事板 运行时必须在两个视图之间执行segue时创建它们 控制器。类型仍然可以以编程方式启动segue performSegueWithIdentifier:sender: UIViewController的方法 想要的东西。您可以这样做,以从添加的源启动一个segue 因此在接口生成器中不可用。
你仍然可以通过编程的方式告诉故事板使用presentModalViewController:或pushViewController:animated:调用的segue来呈现一个视图控制器,但是你需要一个故事板实例。
你可以调用UIStoryboards类方法来获得一个命名的故事板,它带有主bundle的nil。
情节提要名称:捆绑包:
对于故事板中的控制器。
Jhilgert00是你要找的东西吗?
-(IBAction)nav_goHome:(id)sender {
UIViewController *myController = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeController"];
[self.navigationController pushViewController: myController animated:YES];
}
还是……
[self performSegueWithIdentifier:@"loginMainSegue" sender:self];