We have two UIViewControllers
firstViewController
SecondViewController
.When we click on the UItableViewCell we want it to segue to the SecondViewController.
We tried like this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIStoryboard *storyboard;
destinationController = [[UserDetails alloc] init];
destinationController = (UserDetails *)[storyboard instantiateViewControllerWithIdentifier:#"User"];
destinationController.FriendlyName.text=[usernameFriendlyName objectAtIndex:indexPath.row];
UIBarButtonItem * backButton =[[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStylePlain target:self action:nil];
destinationController.navigationItem.backBarButtonItem = backButton;
[self.navigationController pushViewController:destinationController animated:TRUE];
}
Please guide to us .What wrong in our code.
No need to do all this. Just connect the two viewControllers with the Push segue. Give some identifier to segue and simply write this line in didSelectRowAtIndexPath
[self performSegueWithIdentifier: #"Your segue identifier" sender: self];
To pass data from one view to other use PrepareForSegue method
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"Your segue identifier"])
{
// Get reference to the destination view controller
YourViewController *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
[vc setMyObjectHere:object];
}
}
Related
I have an app with a sidebar menu using SWRevealViewController. I followed this tutorial on how to make it, but have a problem. I have many subviews of the first subview that has the prepareForSegue: sender: method. In my subclass view controller, I have the implementation:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
do Saving....
[super prepareForSegue:segue sender:sender];
}
The saving never gets called. I put an NSLog() statement and it was never executed. My layout is the same as the video tutorial (modified for my own app, different view controllers for each table cell). Any help?
Try this
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
SecondViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[navController setViewControllers: #[rootViewController] animated: YES];
It work for me
Or else follow this tutorial you will find solution.
SWReavalview Controller
Try This
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue isKindOfClass:[SWRevealViewControllerSegueSetController class] ]){
UIViewController *dvc = [segue destinationViewController];
UINavigationController *navCtrl = (UINavigationController *) self.revealViewController.frontViewController;
[navCtrl setViewControllers:#[dvc] animated:NO];
[self.revealViewController setFrontViewPosition:FrontViewPositionLeft animated:YES];
}
I have a table. When you click on row of table you get detail using prepare for segue. From detail page, I have an edit button that lets you open a view controller previously created in the storyboard modally.
The question is how can I pass the row of the detail item or otherwise give the edit controller the info of what item to display?
Here is code that launches view controller.
//create Edit navigation button:
UIBarButtonItem *editButton = [[UIBarButtonItem alloc]
initWithTitle:#"Edit"
style:UIBarButtonItemStylePlain
target:self
action:
//next line calls method editView
#selector(editView:)];
self.navigationItem.rightBarButtonItem = editButton;
//method that fires when you click button to launch edit view controller
- (void) editView:(id) sender
{
NSLog(#"pressed");
UIStoryboard *storyBoard = self.storyboard;
NSString * storyboardName = [storyBoard valueForKey:#"name"];
UIViewController *vc = [[UIStoryboard storyboardWithName:storyboardName bundle:nil] instantiateViewControllerWithIdentifier:#"editvc"];
IDEditVC *secondViewController =
[storyBoard instantiateViewControllerWithIdentifier:#"editvc"];
}
But how can I pass the information on the item to edit?
Thanks for any suggestions.
Lets assume that in UITableViewController you are building the TableView an array with objects(eg: MyObject.m/h). You should detect which cell the user has select using the didSelectRowAtIndexPath and then retrieve MyObject from your array using that integer at prepare for segue.
Eg:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
cellPressed =(int)indexPath.row; //cellPressed is an integer variable(global variable in my VC
[self performSegueWithIdentifier:#"toMyVC" sender:self];
}
Now on prepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([[segue identifier] isEqualToString:#"toMyVc"]){
MyVC *mVC = [segue destinationViewController];
mVC.myObject = [myArrayWithMyObjects objectAtIndex:cellPressed];
}
}
In your Edit View:
IDEditVC *secondViewController =
[storyBoard instantiateViewControllerWithIdentifier:#"editvc"];
secondViewController.myObj = myObjectRetrievedFromArray;
Note: You should declare a variable MyObject in .h file in order to be visible from other classes.
Declaring a "global" variable in a class:
#interface ViewController : UIViewController{
int cellPressed; //This is accessible by any method in YourViewController.m
}
I have a problem when clicked a row in a UITableView
I used this method didSelectRowAtIndexPath to catch when the user clicks on a row, but doesn't work normally.
the user must press and hold the row to execute the action but do not want it to be so.
Any idea what may be happening?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
Refunds *current = [refunds_view objectAtIndex:indexPath.row];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
DetailRefundsViewController *myVC = (DetailRefundsViewController *)[storyboard instantiateViewControllerWithIdentifier:#"DetailRefundsViewController"];
[self presentViewController:myVC animated:YES completion:nil];
}
Thanks.
Best Regards.
In my case, your code is instantly work, just modify class name and Storyboard Identifier.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
LJWDetailViewController *myVC = (LJWDetailViewController *)[storyboard instantiateViewControllerWithIdentifier:#"LJWDetailViewController"];
[self presentViewController:myVC animated:YES completion:nil];
}
I think, some of your codes are block this didSelectRowAtIndexPath: Method.
find out which controls are block this method
and set those control's delaysContentTouches Property by NO like this.
_imageScroll.delaysContentTouches = NO;
Sorry for Poor English :<
If you're using StoryBoards, then I think you're better off using a push segue directly from the TableViewCell to the ViewController you want in the Interface Builder (control click and drag from the TableViewCell to the ViewController). Make sure to give the segue a name (in my case below "showDetail"). Then implement the code below:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"]) {
[[segue destinationViewController] setDetailItem:object];
}
}
I'm a very beginner to iOS development. I made a navigation application in iOS to view some details in another view when table view cell tap. I did the following code,
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
LawyerViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:#"LawyerViewController"];
NSLog(#"%#",[self.myObject objectAtIndex:indexPath.row]);
dvc.lawyer = [self.myObject objectAtIndex:indexPath.row];
[self.navigationController pushViewController:dvc animated:YES];
}
This is going to the next view. But dvc.lawyer is not null here. It's null on Next View page.
Reading the Value in Next page
NSLog(#"%#",self.lawyer.firstName);
How can I fix this.
Thanks in Advance!
You are supposed to instantiate ViewControllers in another way, if you use storyboard segues. You need to implement prepareForSegue where you can Access the segues destinationViewController like this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"YOUR_SEGUE_NAME_HERE"])
{
// reference to the destination view controller
LawyerViewController *vc = [segue destinationViewController];
// Pass any data
vc.lawyer = self.myObject[[self.tableView indexPathForSelectedRow].row]; // Not tested
}
}
I am trying to use Table Views and navigation controllers. This is the code I would use if I was using NIB files. This piece of code would work fine if I was using NIB files, but I decided to try storyboard -
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (_webViewController == nil) {
self.webViewController = [[[WebViewController alloc] initWithNibName:#"WebViewController" bundle:[NSBundle mainBundle]] autorelease];
}
RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
_webViewController.entry = entry;
[self.navigationController pushViewController:_webViewController animated:YES];
}
I have created another scene in storyboard and have everything set up out there. When a user clicks a cell in this Table View they will be taken to that scene. This is what I wrote for the code but it is not right and I'm hopelessly stuck :( -
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"articleView"]){
UINavigationController *navigationController = segue.destinationViewController;
WebViewController *controller = (WebViewController *)navigationController.topViewController;
controller.webView = self;
}
Answer - Answers 2 and 3 are both right
I had a very similar problem 2 days ago, with the user selecting the disclosure accessory on a row and wanting to invoke a segue. What worked for me:
In storyboard, create segue "articleView" from the entire tableView controller (not the cell) to the next viewController.
Add the following method to your tableViewController:
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:#"articleView" sender:[self.tableView cellForRowAtIndexPath:indexPath]];
}
Just for you I also tested with tableView:didSelectRowAtIndexPath: and it works.
Enjoy,
Damien
If your table view is already embedded in a navigation controller (looks that way, from your first code snippet) then the destination view controller of your segue will be a WebViewController, not a navigation controller.
You can therefore have your prepareForSegue method pass the item directly across :
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"articleView"])
{
WebViewController *controller = (WebViewController *)segue.destinationViewController;
RSSEntry *entry = [_allEntries objectAtIndex:[self.tableView indexPathForSelectedRow].row];
controller.entry = entry;
}
}
You may find this excellent two-part tutorial helpful
http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1