Presentviewcontroller from Custom Tablecell in Xib

PresentViewController from custom TableCell in xib

TableViewCell is a view, you can not present on views instead UIViewController can handle it. You should transfer the control from your cell to your controller that holds tableview and creates custom cell for it.

Try like this:

Custom Cell .h Class:

@protocol changePictureProtocol <NSObject>
-(void)loadNewScreen:(UIViewController *)controller;
@end

@property (nonatomic, retain) id<changePictureProtocol> delegate;

Then Synthesize it in.m.

Add this in m file:

-(IBAction)changePicture:(id)sender
{
// ..... blah blah
[self.delegate loadNewScreen:picker];
}

The viewcontroller that loads this cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// create cell here

cell.delegate = self;
}

-(void)loadNewScreen:(UIViewController *)controller;
{
[self presentViewController:controller animated:YES completion:nil];
}

Its a psuedocode to give you an idea.

EDIT:

Swift equivalent:

CustomTableViewCell.swift code:

protocol ChangePictureProtocol : NSObjectProtocol { 
func loadNewScreen(controller: UIViewController) -> Void;
}

class CustomTableViewCell: UITableViewCell {

// Rest of the class stuff

weak var delegate: ChangePictureProtocol?

@IBAction func changePicture(sender: AnyObject)->Void
{
var pickerVC = UIImagePickerController();
if((delegate?.respondsToSelector("loadNewScreen:")) != nil)
{
delegate?.loadNewScreen(pickerVC);
}
}
}

ViewController.swift code:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier") as CustomTableViewCell!

cell.delegate = self;

return cell;
}

func loadNewScreen(controller: UIViewController) {
self.presentViewController(controller, animated: true) { () -> Void in

};
}

Load or Push viewcontroller from tableviewcell class

You have to either pass your navigation controller instance through to the collectionView cell and use it there to push(bad, lazy idea)

OR

Create a protocol delegate to inform your ViewController that your CollectionView selected something and push from the ViewController.

Show ViewController from XIB-file-Button - Swift

You can always instantiate a view controller from your Storyboard and present it on button tapped:

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("ViewControllerID") as UIViewController
self.presentViewController(vc, animated: true, completion: nil)

Performing Segue from - TableViewCell.xib to UIView - and - TableView.xib to UIViewController -

Your second approach looks to be right one but of course you are doing something wrong there. You need to drag and drop Segue from Parent ViewController to Detail ViewController and give it a Segue identifier in Storyboard (check attached Image below). IN didSelectRowAtIndexPath you need to do below code:

    self.performSegueWithIdentifier("showDetailViewController", sender: nil)

As I can see you already tried that but it looks like you missed some step either setting Segue Identifier or giving wrong identifier in performSegueWithIdentifier method. Also your Parent ViewController should be embedded in UINavigationController in case you are missing that thing :)
Sample Image

Also keep in mind you should perform navigation (here its Segue) from one ViewController to another ViewController at same level not from one View to another ViewController.



Related Topics



Leave a reply



Submit