is it possible to segue from a UITableViewCell on a UIView to another view

后端 未结 3 1801
挽巷
挽巷 2020-12-10 08:07

Xcode 4.6.1 iOS 6 using storyboards

My problem is this

I have a UITableView with dynamic prototype cells on a UIView in a UIViewController (that is itself e

3条回答
  •  失恋的感觉
    2020-12-10 08:37

    First of all segues can be use only between UIViewControllers. So in case you want to perform a segue between two views that are on the same view controller, that's impossible.

    But if you want to perform a segue between two view controllers and the segue should be trigger by an action from one view (inside first view controller) well that's possible.

    So in your case, if I understand the question, you want to perform a segue when the first cell of a UITableView that's inside of a custom UIView is tapped. The easiest approach would be to create a delegate on your custom UIView that will be implemented by your UIViewController that contains the custom UIView when the delegate method is called you should perform the segue, here is a short example:

    YourCustomView.h

    @protocol YourCustomViewDelegate 
    
    -(void)pleasePerformSegueRightNow;
    
    @end
    
    @interface YourCustomView : UIView {
       UITableView *theTableView; //Maybe this is a IBOutlet
    }
    @property(weak, nonatomic) iddelegate;
    

    YourCustomview.m

    @implementation YourCustomview
    @ synthesise delegate;
    
    //make sure that your table view delegate/data source are set properly
    //other methods here maybe
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        if(indexPath.row == 0) {  //or any other row if you want
           if([self.delegate respondsToSelector:@selector(pleasePerformSegueRightNow)]) {
              [self.delegate pleasePerformSegueRightNow];
           }
        }
    } 
    

    YourTableViewController.h

    @interface YourTableViewController : UIViewController  {
      //instance variables, outlets and other stuff here
    }
    

    YourTableViewController.m

    @implementation YourTableViewController
    
    -(void)viewDidLoad {
      [super viewDidLoad]; 
      YourCustomView *customView = alloc init....
      customView.delegate = self;
    }
    
    -(void)pleasePerformSegue {
      [self performSegueWithIdentifier:@"YourSegueIdentifier"];
    }
    

    You can create any methods to your delegate or you can customise the behaviour, this is just a simple example of how you can do it.

提交回复
热议问题