简体   繁体   中英

Display a new view when button in cell is pressed

I have data displayed in a customized cell view. A cell contains a title and a button. When a user clicks on the button, the page will re-direct to a new view.

Now what I need to do is, when the user clicks on the button (and not the cell), the title of the cell should be displayed on the new view that will be loaded. How should I code this?

Here are a few tips you should try out.

  1. When you push the view controller ( inside the IBaction of that button that gets pressed) to the next view, implement the title of the next view accordingly.

  2. Use tags for the buttons and logically form a link between them and the uiTableViewCell in which you implemented the button.

  3. Set the cell.textLabel.text value to the nextViewController.title .

You must first add button to cell and set button as Accessory of Table Cell :

    myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [myButton setFrame:CGRectMake(50, 100, 20, 10)];
    [cell addSubview:myButton];
    cell.accessoryView = myButton;
    [myButton addTarget:self action:@selector (ActionMethod) 
       forControlEvents:UIControlEventTouchUpInside];

Now in the ActionMethod you have to put the logic for pushing it to a new view :

    -(void) ActionMethod
   {
        myViewController *Obj=[[myViewController alloc]init];
        [self.navigationController pushViewController:Obj animated:NO];
        [Obj release];
   }

Hope it helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM