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

别说谁变了你拦得住时间么 提交于 2019-11-27 03:34:54

问题


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 embedded in a navigation controller) and I want to segue from one specific cell to another view

(Before anyone suggests I should just be using a UITableViewController , I do have other things on the UIView, so i'm set up this way for a reason.)

Now i'm not sure how to go about creating the segue

If I drag from the prototype UITableViewCell to create a segue , all the generated cells automatically call the the segue - when i need only one to do so. This is normal behaviour and I would get around this if i was using a UITableViewController by creating the segue by dragging from UITableViewController and calling [self performSegueWithIdentifier:.... From my didSelectRowAtIndexPathMethod so only the specific cell I want to perform this segue triggers it.

I don't have a UITableViewController in this case - just my UITableView on a UIView that is part of a UIViewController subclass

I've been playing around and I have just discovered that i cannot drag from the UITableView - doesn't let you do that, so that was a deadend.

My only choice that seemed left to me was to drag from the UIViewController

So i tried that and of course XCode throws up an error on the perform segue line telling me i have ... No visible interface for 'LocationTV' declares the selector performSegueWithIdentifier. LocationTv being my tableview subclass.

What is the correct way to attempt to call the new view in this situation

Thank

Simon


回答1:


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 <NSObject>

-(void)pleasePerformSegueRightNow;

@end

@interface YourCustomView : UIView {
   UITableView *theTableView; //Maybe this is a IBOutlet
}
@property(weak, nonatomic) id<YourCustomViewDelegate>delegate;

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 <YourCustomViewDelegate> {
  //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.




回答2:


My Solution

I ended up using a delegation pattern

I made a segue dragging from the my UIViewController - specifically dragging from the viewController icon (the orange circle with a white square in it - from the name bar thats under the view in the storyboard - although you could also drag from the sidebar ) to the view that i wanted to segue to.

I needed to trigger this segue from a table view cell on a table view.

TableView Bit

So i declared a protocol in my tableview header file - which is called LocationTV.h - as follows

@protocol LocationTVSegueProtocol <NSObject>

-(void) makeItSegue:(id)sender;

@end

Below that I declare a property to hold my delegate

@property (nonatomic, strong) id<LocationTVSegueProtocol> makeSegueDelegate;

To actually trigger the segue i called the makeItSegueMethod on my makeSequeDelegate in my didSelectRowAtIndexPath method

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

switch (indexPath.section) {
        DLog(@"selected row %d",indexPath.row);
    case dLocation:
    {
        if(indexPath.row == 2){

            [_makeSegueDelegate makeItSegue:self];

        } else if (indexPath.row == 7){

UIViewController Bit

and set up my UIViewController (named MultiTableHoldingVC) as implementing that protocol

@interface MultiTableHoldingView : UIViewController    
                                  <EnviroTVProtocol,LocationTVSegueProtocol> {

}

Below that i declared the protocol method in the list of my classes methods (although i'm not sure that is necessary as the compiler should know about the method as the decalration of implementing a protocol is essentially a promise to implement this method)

-(void) makeItSegue:(id)sender;

And then over in the implementation file of my UIViewController i wrote the method which essentially just calls preformSegueWithIdentifier

-(void) makeItSegue:(id)sender{ 
    [self performSegueWithIdentifier:@"ChooseCountryNow" 
                              sender:sender];   
}

And to link it all together,as in the header file I had declared my instance of the tableView as follows

@property (strong, nonatomic) IBOutlet LocationTV *dsLocationTV;

I had to set that tables views delegate property to be self - which I did in my UIViewControllers -(void)ViewDidLoad method

_dsLocationTV.makeSegueDelegate = self;

It all seems a bit of a kludge calling a method to call a method and allprog suggestion is simpler (I cant for the life of me work out why it threw up errors for me) but this works just fine . Thanks to both allprog and danypata for their suggestions.

Hope this is helpful to someone out there




回答3:


performSegueWithIdentifier: is a method of the UIViewController class. You cannot call it on a UITableView instance. Make your view controller implement the UITableViewDelegate protocol and set it as the delegate for the UITableView.

Another option is that you don't use segues. In the same delegate method do:

OtherViewController ov = [[OtherViewController alloc] init<<some initializer>>];
// Or in case of storyboard:
OtherViewController ov = [self.storyboard instantiateViewControllerWithIdentifier:@"ovidentifier"];

// push view controller
[self.navigationController pushViewController:ov animated:YES];

If the delegate object is different from the view controller, then the easiest solution is to add a weak property to the delegate's class that keeps a reference to the viewController, like this:

@property (weak) UIViewController *viewController;

and set it up in the viewDidLoad of the viewController

- (void) viewDidLoad {
    self.tableView1.viewController = self;
}

Make sure that the tableView1 property is declared like this:

@property (IBACTION) (weak) SpecialTableView *tableView1;

Sometimes using the storyboard is more painful than writing the code yourself.



来源:https://stackoverflow.com/questions/17103400/is-it-possible-to-segue-from-a-uitableviewcell-on-a-uiview-to-another-view

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