I've a View Controller named as MasterViewController having a tableView implemented in it (as a part of my module). It contains following methods:
- (void)viewDidLoad
- (void)didReceiveMemoryWarning
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Now I want to integrate my Module in My Main Project named as "myProject" having its view controller ViewController.
How Can I init my MasterViewController in ViewController i.e call its init method. My current approach is only loading (void)viewDidLoad method and showing empty table view in my ViewController keeping in view that my MasterViewController have an interface builder of storyboard named as Main
My Approach:
MasterViewController* mainVC = [[MasterViewController alloc] init];
[self.view insertSubview:mainVC.view belowSubview:tabBar];
What you can do is in viewDidLoad of ViewController class add the following;
In Viewcontroll.h:
- (void)viewDidLoad
{
.....
MasterViewController *MSVC = (MasterViewController * )[self.storyboard instantiateViewControllerWithIdentifier:#"Main"];
[self addChildViewController:MSVC];
[self.view addSubview:MSVC.view];
.....
}
Note that you have to call both addChildViewController and addSubview in order for your child view to display. You should not init your MasterViewController in your ViewController's init method.
Related
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
I have a view controller that has two table view controllers as subviews.
When I click one of the cells in the table view controller, I would like it to push to a new view controller. However, it says self.navigationController and self.parentViewController.navigationController are (null).
Does anyone know how I can push a new view from a subview? Thanks!
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
ProductClass *productClass = [arrProducts objectAtIndex:indexPath.row];
ProductSingleViewController *productSingleViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"ProductSingleViewController"];
productSingleViewController.prod_title = productClass.title;
NSLog(#"VC1: %#, VC2: %#", self.navigationController, self.parentViewController.navigationController);
[self.parentViewController.navigationController pushViewController:productSingleViewController animated:YES];
}
One way to handle this is by setting a delegate method inside the UITableViewControllers. Add the delegate method to your parent view controller. It will get triggered whenever someone taps a cell inside the tables. From this delegate method you will be able to push a new view controller onto the stack.
In MyTableViewController.h:
#protocol MyTableDelegate <NSObject>
#required
- (void)selectedCellWithInfo:(NSDictionary *)info
#end
#interface MyTableViewController : UITableViewController {
id <MyTableDelegate> delegate;
}
#property (strong) id delegate;
In MyTableViewController.m:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[delegate selectedCellWithInfo:[myTableDataSource[indexPath.row]];
}
In MyMainViewController.h:
#import "MyTableViewController.h"
#interface MyMainViewController : UIViewController <MyTableDelegate>
In MyMainViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
myTableViewController.delegate = self;
}
- (void)selectedCellWithInfo:(NSDictionary *)info {
// Do something
}
The navigationController property of a view controller will return a valid navigation controller object only if the view controller is in a navigation controller's navigation stack.
If the PRoductSingleViewController is your rootviewcontroller
1. Drag and drop the navigation controller
2. set the navigation controller as rootviewcontroller.
3. Replace the navigation child Viewcontroller with your PRoductSingleViewController
Or you can manage it in app delegate.m class
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
_productSingleViewController = [[PRoductSingleViewController alloc]initWithNibName:#"PRoductSingleViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: _productSingleViewController];
[self.window addSubview:navController.view];
[navController release];
[self.window makeKeyAndVisible];
return YES;
}
I have a UIViewController which has another UIViewController added as a subview:
#import "ListViewController.h"
#interface SearchViewController ()
#end
#synthesize listController;
- (void)viewDidLoad {
[super viewDidLoad];
…
self.listController =[[ListViewController alloc] init];
[self.view insertSubview:listController.view belowSubview:mainTabBar];
self.listController.view.frame = CGRectMake(0, tabHeight, screenWidth, (screenHeight-tabHeight));
}
#end
The ListViewController has a TableView in it. On the click of an item in the TableView, I want to add a UIViewController to the Navigation Controller:
#import “PlaceViewController.h"
#interface ListViewController ()
#end
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
PlaceViewController *vc = [[PlaceViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#end
This works if the ListViewController is added to the Navigation Controller normally. But, in this case, when the ListViewController is basically "nested" within another, the newly added PlaceViewController is not opening. Is there a way to make this work? Thanks
You should create a parent/child relation between the container and the controller which has its view added. Some informations can be found in the intro of https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/ and more on the internet. Just look at custom container view controller.
Then you can get the navigationController property of your container from your ListViewController using the parentViewController property of ListViewController.
objective-c and ios newbie here but I'm learning!
name is a string in descs. I'm trying to pass it to a new view controller through detailViewController.note. I can see in debugging that the string is being saved in detailViewController.note ,but when I go over to my other view controller it is not there.
here is the code from my table view controller:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
DetailViewController *detailViewController = [[DetailViewController alloc]init];
detailViewController.view.frame=self.view.frame;
detailViewController.title=#"detail";
detailViewController.note=[self.descs[indexPath.row]name];
[[self navigationController] pushViewController:detailViewController animated:YES];
}
Header for DetailViewController:
#interface DetailViewController : UIViewController
#property (nonatomic) NSString * note;
#end
Implementation for DetailViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
UITextView *description =[[UITextView alloc]initWithFrame:CGRectMake(12,260,300,180)];
description.font=[UIFont fontWithName:#"Helvetica" size:15];
description.editable=NO;
description.text=self.note;
[self.view addSubview:description];
}
Thanks in advance for your help!
You should set up your subview in viewWillAppear:. It's not working in your current implementation because viewDidLoad is called before detailViewController.note has been set.
I have a UIViewController in which I am adding a subclass of a UITableView as a subview.
e.g.
#interface ViewController : UIViewController
#interface ItemList : UITableView
in ViewController viewDidLoad I create a ItemList (itemList) object and add it to the view of the viewcontroller
- (void)viewDidLoad {
...
[self.view addSubview:itemList];
}
in the didSelectRowAtIndexPath of ItemList which is a UITableView I do not have access to the navigation controller so how do I push another viewcontroller on to the navigationcontroller?
#implementation ItemList
{
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.navigationController pushViewController:newViewControllerobject animated:YES];
}
}
Create a property eg : ViewController *mydelegate in itemList class.
when you add the itemList in ViewController's viewDidLoad add the following
itemList.myDelegate = self;
Moving ahead..on did select
#implementation ItemList
#synthesize mydelegate;
{
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[myDelegate.navigationController pushViewController:newViewControllerobject animated:YES];
}
}
If you have very first navigationController defined in AppDelegate, you can use the following code to find uppermost navigationController and then push the viewController upon it.
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
[appDelegate.navigationController.visibleViewController.navigationController pushViewController:newViewControllerobject animated:YES];