Segue Back to Self View Controller - objective-c

I am currently facing a problem that I wanted to segue back to the same view controller by click on a table view cell. For your information, my table view is generated programatically and each of the cells are using NIB files to control.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath (NSIndexPath *)indexPath
{
if( indexPath.row == 0 ){
NSLog(#"Clicked");
}
}
When I clicked on the cell at position number 0, the log does came out. So now I wanted to segue back to the same view controller instead to the others. From the storyboard, it is impossible for me the drag the segue it is because the view controller is only have a scroll view.
E.g: Inside View Controller A have 5 cells, when I clicked on the first cell, it will segue back to View Controller A and pass data.
Please advise ya if you need to see more of my codes. Thanks viewer!

If I understand you correctly, you want to select a row in your tableView and have it performSegueWithIdentifier back to this same/current UITableViewController?
To do this:
In storyboard, ctrl+drag from the exposed table view cell to the yellow view controller icon in the black bar below. This creates the segue to self view controller.
Click on the segue, and add an identifier; e.g. "segueToSelf"
In your didSelectRowAtIndexPath method, add the following:
[self performSegueWithIdentifier:#"segueToSelf" sender:self];

Related

unwind segue programatically (back to root view controller from table view cell selection)

I m new to storyboard, in my application i have story board with name "LeftMenuList" in this i have created table view controller manually and place table view to self.view, now i wan't unwind segue from table view cell selection for root view controller.
For this i read multiple document related to unwind segue.
https://www.youtube.com/watch?v=Zc8Ez9YfBSM
http://spin.atomicobject.com/2014/12/01/program-ios-unwind-segue/ etc..
but in every document button's are created using storyboard and for connection just cntr+drag from button to exit.
But as i said earlier my table view is created manually so i m not able connect selected cell to exit for creating unwind segue (go back to login view), so can any buddy help me how can i solved my issue..For more clarification i will attach screen shot.
Code :---
1) In Viewcontroller.m (root View-Login Screen) added below method as i wan't this view on unwind segue:-
-(IBAction)UnwindSegueToRootViewController:(UIStoryboardSegue *)segue
{
}
2) In leftMenuList.m (in this view controller i m having table view cell on-- cell selection(logout) need to go back login screen -- so below is didSelectRowAtIndexPath)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.strSelectedOption = [self.arrOfMenu objectAtIndex:indexPath.row];
if ([self.strSelectedOption isEqualToString:#"Logout"])
{
[self performSegueWithIdentifier:#"UnwindSegueToRootViewController" sender:self];
// [self shouldPerformSegueWithIdentifier:#"UnwindSegueToRootViewController" sender:self];
}
}
And below Screen shot shows identifier connection

CollectionView CollectionCell segue to DetailView

In my CollectionView I display two “object types” in CollectionCells. What I would like to do is when the user clicks on a cell, in the “didSelectItemAtIndexPath” determine the object selected and use “performSegueWithIdentifier” to segue to the appropriate DetailTableViewContoller to display the details of the “object”.
The way I have the setup is NavigationController segue (root) to MyCollectionView segue (push) to DetailTableView. The CollectionCell in the MyCollectionView is configured to segue to the DetailTableView.
Starting simply, when I have the following code in place, select a cell in MyCollectionView it segues to the DetailTableView correctly but when I select the Back button in the DetailTableView it fails.
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:#"MySegueIdentifier" sender:self];
}
I get the dreaded
[6313:a0b] nested push animation can result in corrupted navigation bar
[6313:a0b] Finishing up a navigation transition in an unexpected state.
Navigation Bar subview tree might get corrupted.
If I comment out the performSegueWithIdentifier… line it segues correctly and the Back button in the DetailTableView works perfectly.
Similar code pattern works fine in TableViewController seque to DetailViewController.
Any suggestions? Is it proper to set the segue up from the CollectionCell to the DetailViewController?
Figured this out. Having the segue at the cell is incorrect. Setting it at the Controller level is correct.

Dismiss UITableViewController on tableView:didSelectRowAtIndexPath:indexPath

Currently I have a UIView with a UItableview cell added to the subview, like so,
When the button is pressed, it pushes to another navigation controller whos subclass is a uitableviewcontroller....
So When the user clicks on one of these tableviewcells... I want the viewcontroller to be poped from the navigation stack and go back to the previous view that is listed first.
I implemented the UITableviewcontroller method listed below with the following implementation.... but nothing occurs :P....
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self dismissViewControllerAnimated:YES completion:nil];
}
Any ideas?
If you are pushing the view controller onto the navigation controller's stack i.e. pushViewController:animated:, you should pop out current view controller using popViewControllerAnimated: method.
If you want to go to a view controller not just one level below the current view controller (or top view controller in UINavigationController's jargon), you can use popToViewController:animated:. And to go to the root view controller of the navigation controller, use popToRootViewControllerAnimated:.
But if you are presenting a view controller modally i.e. presentViewController:animated:completion:, then only you use the dismissViewControllerAnimated:completion: method to dismiss the presented view and go to the presenting view controller.

didSelectRowAtIndexPath not called on tableView

I am using tableView in a viewcontroller1 and implementing a didSelectRowAtIndexPath for moving to viewController2 with some data and displaying it on a tableView on viewController2...for now everything is ok, but then when I implement the code on viewController2:
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath) {
// some code
}
After I selecting a row its selected and colored in blue but never stop's in the method didSelectRowAtIndexPath.
What can be the problem?
You should set not only the data source of the table view to your view controller, but the delegate also:
tableView.delegate = self;
You need to set the delegate of your tableView, either in your storyboard or XIB file, or programatically.

Show different view on navigation controller when data is empty using storyboard

I have a navigation controller with a tableview controller as the first controller, however when the database is empty I want to show the user a view that asks the user for some information to fill the database and show it in the navigation controller instead of the view assigned in storyboard, I guess I would have to do this programmatically, but I'm new to iphone development and honestly dont know where to start.
Here's how you can do it. Create a new scene in your storyboard for your view controller where the user will submit the info (let's say it's called CollectInfoViewController). Then create a modal segue from your tableview controller to the CollectInfoViewController. In the Attributes Inspector, set the Identifier property of the segue to "CollectInfo" so you can identify it later.
Then when your tableview controller's viewDidLoad method runs, ask the table view delegate whether there are any rows to show. If there are not, programmatically perform the segue to your CollectInfoViewController.
- (void)viewDidLoad
{
[super viewDidLoad];
if ([self.tableView.dataSource tableView:self.tableView numberOfRowsInSection:0] == 0) {
[self performSegueWithIdentifier:#"CollectInfo" sender:self];
}
}
In your CollectInfoViewController, once the user has entered the information and you have stored it in your database then you can call the following from within CollectInfoViewController to dismiss that view. This will return the user to the tableview.
[self dismissModalViewControllerAnimated:YES];
Finally, back in your tableview controller, you might need to reload your table with the new data that's been collected. You can do that in viewWillAppear, which will be called when the modal view controller is dismissed.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView reloadData];
}