use prepareForSegue for button

梦想的初衷 提交于 2019-12-10 19:20:40

问题


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

Thanks in advance!

here is my code

Creating Button

 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];

Action for 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];


}

Prepare for segue

- (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
    }}

error

Edit 2:**** I have a warning :

Segues initiated directly from view controllers must have an identifier for use with -[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"];
    }}

回答1:


There are really two ways to push a view controller onto the navigation controller.

  1. The old way, all in code. That is what you did. You can completely forget about segues at this point.

  2. 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];

where self is the view controller. Now you can do the customizations in prepareForSegue:.




回答2:


the sample code in this case is just telling you the type of thing you can do in prepareForSegue:sender: .

it is suggesting that the viewController to which you are attempting to segue would have a method setMyObjectHere: and it would be expecting you to pass that object.

as an example, your class might have a setTitle: method, and in the prepareForSegue:sender: code, you could pre-set the title based on information in the controller you are performing the segue from.

to get rid of the warning, you need an identifier, as in the picture below:




回答3:


"The old way to code", no the better way to code hopefully this helps, but if your using UIButtons and UIViews programmatically you shouldn't even be using segues.

Create a root view control in app delegate, than just pop on and off the next viewcontrollers and such.

For example this is all you need in your viewcontrollers:

-(void)nextControllerButton:(UIButton*)button{

    SearchViewController *nextController = [[SearchViewController alloc] init];

    nextController.username = _username.text;
    nextController.password = _password.text;

    [self.navigationController showViewController:nextController sender:self];
}


来源:https://stackoverflow.com/questions/11135047/use-prepareforsegue-for-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!