对按钮使用 prepareForSegue [英] use prepareForSegue for button
问题描述
我以编程方式创建按钮,并将按钮连接到另一个视图,但遇到了segue 问题我应该对故事板使用 prepareForSegue 方法,但我不知道互联网上如何有一些示例,但是当我使用该示例时会出错,请您帮帮我
Hi I create button programmatically and I connect my button to another view, but I got segue problem that I should use prepareForSegue method for storyboard but I don't know how there ar esome sample to the internet but I will get error when I used that sample, would you please help me
提前致谢!
这是我的代码
创建按钮
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor=[UIColor colorWithRed: 201.0/255.0 green: 201.0/255.0 blue:201.0/255.0 alpha: 1.0];
button.tag = currentTag;
currentTag++;
[button.layer setBorderColor: [[UIColor blackColor] CGColor]];
[button.layer setBorderWidth: 1.0];
[button setTitle:[NSString stringWithFormat:@"%d",currentTag] forState:UIControlStateNormal];
button.frame = CGRectMake(80*x, 32*y, 80, 32);
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[buttonView addSubview: button];
按钮操作
-(void)buttonPressed:(UIButton *)button
{
NSLog(@"button %u -- frame: %@", button.tag, NSStringFromCGRect(button.frame));
WeekView *crtObj=[[WeekView alloc]initWithNibName:@"WeekView" bundle:nil];
[self.navigationController pushViewController:crtObj animated:YES];
[crtObj release];
}
准备转场
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"WeekView"]) {
// Get reference to the destination view controller
WeekView *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
[vc setMyObjectHere:object]; //// I don't know what should I write instead of object
}}
错误
编辑 2:**** 我有一个警告:
直接从视图控制器启动的 Segue 必须有一个标识符才能与 -[UIViewController performSegueWithIdentifier:sender:] 一起使用:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"WeekView"]) {
// Get reference to the destination view controller
// WeekView *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
// [vc setMyObjectHere:object];
[segue.destinationViewController setTitle:@"WeekView"];
}}
推荐答案
实际上有两种方法可以将视图控制器推送到导航控制器上.
There are really two ways to push a view controller onto the navigation controller.
老方法,全在代码中.那就是你所做的.此时您可以完全忘记 segue.
The old way, all in code. That is what you did. You can completely forget about segues at this point.
新方法:在 Storyboard 中建立一个 Segue(将 segue 从一个视图控制器推送到一个新的视图控制器,您也在 Storyboard 中定义),给它一个 Identifier
名称,然后使用此代码推送视图控制器.
The new way: establish a Segue in Storyboard (push segue from one view controller to a new view controller, which you also define in Storyboard), give it an Identifier
name and then use this code to push the view controller.
.
[self performSegueWithIdentifier:myIdentifyer sender:self];
其中 self 是视图控制器.现在您可以在 prepareForSegue:
中进行自定义.
where self is the view controller. Now you can do the customizations in prepareForSegue:
.
这篇关于对按钮使用 prepareForSegue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!