Different custom View controller for iOS splitViewcontroller - ios

I have a splitview controller containing master and detailview controller. But now i need to display different custom view controller based on the selection in the masterviewcontroller (a tableview). How can i achieve this?

Write below code in master view controller.
Take the decision of creating custom view based on indexpath value.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UISplitViewController *splitController = self.splitViewController;
NSMutableArray *arr = [splitController.viewControllers mutableCopy];
CustomViewController *customVC = [[CustomViewController alloc]init];
[arr replaceObjectAtIndex:1 withObject:customVC];
[customVC release];
splitController.viewControllers = arr;
}

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

Two view controllers use same table view

I'm new to iOS development. I want to know how to pass back data from same tableview to two different view controller.
This is my didselectrow method:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
sendarea = [tableData objectAtIndex:indexPath.row];
Step2ClassifiedViewController *parentViewC = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 2];
parentViewC.areareceive = sendarea;
[self.navigationController popViewControllerAnimated:YES];
}
But this is only for one view. I have another view also need to pass back the data if i use the same table. Is that possible? or do i just need to create another table?.
You can use same table. Only you need change the data source. And can call in each didSelectRowAtIndexpath
This is just like, A taxi driver carries passenger by his one taxi. When his taxi is empty,he can carries passenger.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
sendarea = [tableData objectAtIndex:indexPath.row];
if(state == 1){
Step2ClassifiedViewController *parentViewC = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 2];
parentViewC.areareceive = sendarea;
[self.navigationController popViewControllerAnimated:YES];
}
else if (state == 2){
//get your other View Controller and set data before pop to that view controller.
NSArray *viewControllers = [[self navigationController] viewControllers];
for(UIViewController *vc in viewControllers){
if([vc isKindOfClass:[OtherViewController class]]){
OtherViewController *otherVC = (OtherViewController*)vc;
otherVC.areareceive = sendarea;
[[self navigationController] popToViewController:otherVC animated:YES];
}
}
}
else{
//load any View Controller you want
}
}
Based on your question and the comments, I assume that you want to load different View Controllers based on your different state and pass along data.
The above is one way of doing that. However, you could also use delegation for your purpose, it would be a good programming practise.

ios - naming and make action from each cell

So I'm using this code... "MFSideMenuDemoStoryboard"
https://github.com/mikefrederick/MFSideMenu/tree/master/Demos/MFSideMenuDemoStoryboard/MFSideMenuDemoStoryboard
And I want to give a name to each cell in the "SideMenuViewController.m" and make each one of these cells show a specific UIView in the "DemoViewController.m". any help with that ?
any help is appreciated, thank you!!
In SideMenuController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DemoViewController *demoController = [[DemoViewController alloc] initWithNibName:#"DemoViewController" bundle:nil];
demoController.title = [NSString stringWithFormat:#"Demo #%d-%d", indexPath.section, indexPath.row];
UINavigationController *navigationController = self.menuContainerViewController.centerViewController;
NSArray *controllers = [NSArray arrayWithObject:demoController];
navigationController.viewControllers = controllers;
[self.menuContainerViewController setMenuState:MFSideMenuStateClosed];
}
Replace this code with something like:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//EDIT #1
UINavigationController *navigationController = self.menuContainerViewController.centerViewController;
DemoViewController *currentCenterView = [[navigationController viewControllers] firstObject];
[currentCenterView addAnotherView:indexPath.row]; //This is where you will tell your 'middle view' to load another UIView ... You can use the indexPath.row to determine which option was tapped and act accordingly.
[self.menuContainerViewController setMenuState:MFSideMenuStateClosed];
}
You obviously need to add a method - (void) addAnotherView:(NSInteger ) row; to your DemoViewController or whatever you've called it.
Edit #1 this will reference the current loaded view. This will only work if you are only using 1 view controller on the navigation controller stack. Otherwise grabbing the first object won't work and you'll need to search for the right index.

IOS - get a value from a table view controller and send it to another table view controller

I have two Table View Controller in a Navigation Controller. The Cloth table is the main view and when I click on one of the cells go to another view controller that gives me the detail, when I select the option i want and return to the Return of the Navigation button, I lose the information. How do I take the value to the main view?
Here my code: Main View - AddClothViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[list objectAtIndex:indexPath.row] isEqual:#"Categorias"] )
{
CategoriesViewController *DVC = [[CategoriesViewController alloc] initWithNibName:#"CategoriesViewController" bundle:nil];
[self.navigationController pushViewController:DVC animated:YES];
}
}
Here my code: Categories ViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Set the checkmark accessory for the selected row.
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
// This is my main View Controller when i declare a variable category and I try to access from
//here
AddClothViewController *DVC = [[AddClothViewController alloc] initWithNibName:#"AddClothViewController" bundle:nil];
DVC.clothNew.categoria = [list objectAtIndex:indexPath.row ];
}
I resolved my problem by using this tutorial.
(Simple delegate tutorial for ios development)

Editing a tableview in a tabbed application

I have a tabbed view application which uses a tab controller as the root view controller. One of the views I have is a tableview with I want to be able to edit by clicking on an add/edit button that will make all the cells editable as well as allowing to add a cell.
In the table view I have added a navigation bar and have tried to use the following code to create a button on the bar that when pressed will make all the cells editable:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *editButton = [[UIBarButtonItem alloc]
initWithTitle:#"Delete"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(toggleEdit:)];
self.navigationItem.rightBarButtonItem = editButton;
}
-(IBAction)toggleEdit:(id)sender {
[self.mTableView setEditing:!self.mTableView.editing animated:YES];
if (self.mTableView.editing)
[self.navigationItem.rightBarButtonItem setTitle:#"Done"];
else
[self.navigationItem.rightBarButtonItem setTitle:#"Delete"];
}
The code doesn't work, it doesn't produce a button on the navigation bar. I have read that I should use a navigation controller but if I wish to have this one page (in my tabbed view app) use a navigation controller how do I get the tab bar to point to the nav controller as a selection option?
----Update-----
Ok Ive made a nib file that contains the root view controller (a tab bar controller). The tab bar controller contains 2 view controllers and a navigation controller with a view controller in it. I link the View controller in the Navigation controller to a View that contains a tableview. When I run the program and try to click on the tab for the tableview I get the following error:
2012-04-29 17:23:28.116 ash[6778:f803] -[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x68bb8d0
2012-04-29 17:23:28.117 ash[6778:f803] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x68bb8d0'
My view controller that contains the tableview is a UIViewController that implements
In its m file it contains the following methods:
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [self.portfolioArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"CellTableIdentifier";
static BOOL nibsregistered = NO;
if (!nibsregistered) {
UINib *nib = [UINib nibWithNibName:#"ASHInstrumentCell" bundle:nil];
[tableView registerNib:nib forCellReuseIdentifier:SimpleTableIdentifier];
nibsregistered = YES;
}
ASHInstrumentCell *cell = [tableView dequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
NSUInteger row = [indexPath row];
cell.type.text = [portfolioArray objectAtIndex:row];
return cell;
}
#pragma mark -
#pragma mark Table Delegate Methods
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
NSString *rowValue = [portfolioArray objectAtIndex:row];
NSString *message = [[NSString alloc] initWithFormat:
#"You selected %#", rowValue];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Row Selected!"
message:message
delegate:nil
cancelButtonTitle:#"Yes I Did"
otherButtonTitles:nil];
[alert show];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
NSUInteger row = [indexPath row]
[self.portfolioArray removeObjectAtIndex:row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic] ;
}
What am I doing wrong?
Two points: First of all, a UINavigationController is a subclass of UIViewController, so you could create an instance of UINavigationController whose rootViewController is your tableViewController, and have the TabBarController manage the nav controller as one of its tabs.
Also note that the UIViewController class already has a editButtonItem property that you should use instead of creating your own button:
self.navigationItem.rightBarButtonItem = self.editButtonItem;
Ok I think I figured out what I was doing wrong. I had created a nib file for my tab view controller and to this I added to view controllers and a Navigation controller (which itself contained a tableview controller).
One of the things I wasn't doing was allocating the correct class type to the view controllers in my tabs. If I have created a custom view controller class firstviewcontroller which had IBOutlets and I didn't specify the view controller in my tab controller nib to be of class firstviewcontroller I would get that weird key value error.
The second problem I had was that the tab view controller wouldn't autorotate even though all the view I wanted to show allowed for autorotation using the following code:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
The reason for this again was that I hadn't specified the class of each of the view controllers (in my tabbedviewcontroller nib file) to be of my custom made classes that I wished them to show.
Once I specified their class type everything fell into place.

Resources