Exit From MMDrawerController Left View Controler - ios

I am using MMDrawerController. I used TableView in LeftViewController to display my data in Custom Cells.
LeftViewController slides out from left when i click the bar button.
i want to redirect to other ViewControler from LeftViewController after clicking on custom cell of my TableView.
in my LeftViewController i am using the below code
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"CenterViewController" sender:self];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
ViewController *VC = (ViewController *)[segue destinationViewController];
}
Note: CenterViewController is the Storyboard ID of ViewController
by using the above code, ViewController appears on top of LeftViewController but i want to close LeftViewController and redirect to ViewController
i used the below code, Although it works but then swipe left and right does not work on ViewController
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ViewController *ggg = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewController"];
UINavigationController *navcon = [[UINavigationController alloc] initWithRootViewController:ggg];
AppDelegate *app =[[UIApplication sharedApplication] delegate];
app.window.rootViewController = navcon;
[app.drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}

I think you have the following options:
Implement the UITableViewDelegate in CenterViewController and assign the CenterViewController instance as the delegate of the UITableView in LeftViewController.
Create a delegate for LeftViewController so you can keep your UITableViewDelegate's logic in LeftViewController and implement the new delegate in CenterViewController and assign it as delegate of LeftViewController.
Use NSNotificationCenter to post a notification when the user selects an item in LeftViewController's UITableView and add CenterViewController as an observer of the notification.
Update 1
HI, In the code I see you are assigning a new rootViewController probably replacing the instance of MMDrawerController with your own ViewControler and because of that you loose the swipe gesture.
Maybe the only thing you need is to assign the center controller to MMDrawerController calling setCenterViewController:
instead of
app.window.rootViewController = navcon;
use
[drawerControllerInstance setCenterViewController:navcon];

Related

UISearchBar Staying at top when seguing from tableview results

I created a search with 2 UINavigationcontrollers and with 2 UITableViewControllers. One for displaying the initial results, and then another TableViewController for when displaying searched results.
This is where i think the issue occurs. I use this method to show the resultsTableViewController where i make it the top view controller.
Now when i segue from the results view controller, the UISearchBar stays on top. When i code [vc.tableView setDelegate: self];.
it removes the issue of the search bar staying but then screws up the didselectrowatindex path which i need to identify because the results aren't the same from the original and the results Tableviewcontroller.
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
// Set searchString equal to what's typed into the searchbar
NSString *searchString = self.searchController.searchBar.text;
[self updateFilteredContentForAirlineName:searchString];
// If searchResultsController
if (self.searchController.searchResultsController) {
UINavigationController *navController = (UINavigationController *)self.searchController.searchResultsController;
// Present SearchResultsTableViewController as the topViewController
SearchResultsTableViewController *vc = (SearchResultsTableViewController *)navController.topViewController;
// Update searchResults
vc.searchResults = self.searchResults;
// And reload the tableView with the new data
[vc.tableView reloadData];
}
}
Here's a project i mocked up that has the issue included.
http://expirebox.com/download/10533c5d386618b95bb39bd1dc886ace.html
try to extend your next viewcontroller to class.so i will create new data class and extend to viewcontroller than paste this code in didSelectRowAtIndexPath: method
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
data *next=[self.storyboard instantiateViewControllerWithIdentifier:#"datascreen"];
[self presentViewController:next animated:YES completion:nil];
}
ok bro try this
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
data *next=[self.storyboard instantiateViewControllerWithIdentifier:#"datascreen"];
[self.navigationController next animated:YES];
}
both class use this code.try this if any problem than tell me

xcode/ios: push view controller programatically and pass row info

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
}

Segue not passing data to destination

I have two table view controllers, one containing all the US states, the other presenting a list of data for the selected state. I have set up a segue in my main storyboard from the view controller (not the cell), and given it an identifier. For reasons beyond the scope of this post, I need to programmatically perform the segue without using the prepareForSegue method.
The source table view controller (AllStatesTableViewController) needs to execute the segue in the didSelectRowAtIndexPath method of the tableview.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
stateID = [statesDict objectForKey:cell.textLabel.text];
DetailTableViewController *destination = [[DetailTableViewController alloc] init];
stateGauges = [[GaugeList alloc] initWithStateIdentifier:stateID andType:nil];
// at this point stateGauges is NOT nil
[destination setStateGauges:stateGauges];
[self performSegueWithIdentifier:#"sgShowStateDetail" sender:self];
}
The segue performs as intended in that it navigates to its destination, but stateGauges is nil in the destination table view controller. It is not nil in the source table view controller. stateGuages is declared as a property in DetailTableViewController. Obviously I am doing something wrong. Any suggestions?
Thanks! V
On your storyboard you need to choose to push your segue from your cell.
and use your prepareForSegue to pass data :
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"showDetail"])
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DetailTableViewController *detailTableViewController = segue.destinationViewController;
// Pass data here
destViewController.stateGauges = stateGauges;
}
}
Have a look here for more information
I ran into this before too. I found that when I let the storyboard handle the segue, it instantiates a new view controller. This is why stateGauges is nil, because you set stateGauges in your programmatically created view controller, not the one the storyboard created. If you won't use prepareForSeque, remove the segue from your AllStatesViewController to your DetailTableViewController in your storyboard. Then programmatically instantiate the storyboard, the view controller, setGauges, then present your view controller. Also, make sure you put "ShowStateDetail" in the "Storyboard ID" field, in the Identity section, on that DetailTableViewController in your storyboard.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
stateID = [statesDict objectForKey:cell.textLabel.text];
DetailTableViewController *destination = [[UIStoryboard storyboardWithName:#"yourStoryBoardName" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:#"ShowStateDetail"];
stateGauges = [[GaugeList alloc] initWithStateIdentifier:stateID andType:nil];
[destination setStateGauges:stateGauges];
[self presentViewController:destination animated:YES completion:nil];
}

Table View View Controller Show Next View

Im working with table view controllers within story board.
I currently have one tableview controller embedded in navigation controller in my storyboard. This table view controller is linked to an ObjectiveC file.
Through code i can display another table view controller with the correct data using the tableView didSelectRowAtIndexPath method.
code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
WorkoutType *workoutType = (WorkoutType *)[self.fetchedResultsController objectAtIndexPath:indexPath];
WorkoutSetViewController *detailViewController = [[WorkoutSetViewController alloc] initWithWorkoutType:workoutType];
[self.navigationController pushViewController:detailViewController animated:YES];
}
But if i try to add another table view controller in storyboard, associate it with an objectiveC view controller file and connect the first TVC to it, That table view controller is not shown upon running the code.
I need to customise the second table view visually and have tried to use a segue push...as follows:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"show"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
WorkoutType *workoutType = (WorkoutType *)[self.fetchedResultsController objectAtIndexPath:indexPath];
//WorkoutSetViewController *detailViewController = [[WorkoutSetViewController alloc] initWithWorkoutType:workoutType];
WorkoutSetViewController *destViewController = segue.destinationViewController;
destViewController.workoutType = workoutType;
//[self.navigationController pushViewController:detailViewController animated:YES];
}
}
But when i run this code, the second table view controller doesn't even load although the application doesn't crash.
Whatever I am doing wrong must be simple, any help would be appreciated.
Thank You
Simple You have to navigate through below code. (No need to set storyboard when you working in same storyboard ViewController)
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexpath.row==0)//for first row click
{
WorkoutSetViewController *objWorkoutSetVC=[[WorkoutSetViewController alloc] i nitWithNibName:#"WorkoutSetViewController" bundle:nil];
[self.navigationController pushViewController:objWorkoutSetVC animated:YES];
}
else if(indexpath.row==1)//for second row click
{
//Put code in which screen you have to navigate
}
}
Do NOT Alloc init your new view controller. Use this code instead:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
WorkoutSetViewController *detailViewController = (WorkoutSetViewController *)[storyboard instantiateViewControllerWithIdentifier:#"coolID"];
[self.navigationController pushViewController:detailViewController animated:YES];
For MainStoryboard write your storyboard name and for coolID write your view controller id name which you set in your storyboard.

Pushing a view in Storyboard

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

Resources