Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...