Embed navigation controller in modal view contriller programatically - ios

I'm presenting a modal view controller using following code,
[[mInfo objectForKey: kNavigationController] presentViewController:(UIViewController*)modalViewControlr animated:YES completion:nil];
UITableView. On selecting a table view cell I want to navigate it to another UIView called navigatedView.
Is this can be done by embedding self (i.e,modalViewControlr) in navigation Controller and add view (i.e, navigatedView) to a view controller and present it?
for example,
// in modalViewControlr
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//
UINavigationController *passcodeNavigationController = [[UINavigationController alloc] initWithRootViewController:self];
UIViewController *vc = [[UIViewController alloc]init];
[vc.view addSubview:self.navigatedView];
self.navigatedView.frame=vc.view.frame;
[passcodeNavigationController pushViewController: vc animated:YES];
//
}
Please help....

I prefer do it in this way which is easier to understand.
1.Embed the navigationController in AppDelegate:
ModalViewControlr *vc = [[ModalViewControlr alloc]init];
UINavigationController *passcodeNavigationController = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = passcodeNavigationController;
2.Create a new class NavigatedViewControllerand custom the controller in viewDidLoad
Create an UIView call navigatedView
#property (nonatomic, strong) UIView *navigatedView;
-(void)viewDidLoad
{
[super viewDidLoad];
_navigatedView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.navigatedView.backgroundColor = [UIColor redColor];
[self.view addSubView:self.navigatedView];
}
3.Then return to modalViewControlr where you want to push from.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NavigatedViewController *vc = [[NavigatedViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
}

Related

How to call a UIViewController from a UIView SubClass?

I have main UIViewController in which I have an UITableView.
In the cell of UItableView I had already add an UICollectionView.
in -
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
I am calling an method of main UIViewController.
UIViewController *view = [[UIViewController alloc] init];
[view my_method];
in main view.
-(void)my_method {
AnotherName *view_my=[[AnotherName alloc]initWithNibName:#"AnotherName" bundle:nil];
[self.navigationController pushViewController:view_my animated:NO];
}
Viewdidload method is calling but view is not showing.
It is because you initialise by calling [[UIViewController alloc] init]. This always returns empty view.
You should instead call this
If you use storyboard give the view controller a storyboard id and call this
MainViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"storyboard id"];
If you use interface builder, call this
MainViewController *controller = [[MainViewController alloc] initWithNibName:#"Nib File Name" bundle:nil];
i got solution..
AnotherName *view_my = [[AnotherName alloc]initWithNibName:#"AnotherName" bundle:nil];
self.window.rootViewController = view_my;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
Spacial thanks to #The Mech System .. To help me out find the solution ..
now it is working fine.

Pushing new UIViewController from custom UIPopOver issue

Hello I'm using this library to show my PopOver which contain tableView with searchBar.
The problem is that one user try to select row from the tableView instead dismissing the popOver and displaying the slected row in hole screen I got this :
.
I've tried to use [self.parentViewController.navigationController ...] to push my view but didn't worked.
This is my popOver code (in MainVC.h):
-(IBAction)showPopoverSearch:(id)sender{
UIBarButtonItem *btn = (UIBarButtonItem *) sender;
NSInteger width = 600;
NSInteger height = 400;
SearchViewController *searchVC = [self.storyboard instantiateViewControllerWithIdentifier:#"searchView"];
searchVC.modalInPopover = NO;
UINavigationController* contentViewController = [[UINavigationController alloc] initWithRootViewController:searchVC];
popoverController = [[WYPopoverController alloc] initWithContentViewController:contentViewController];
popoverController.delegate = self;
//popoverController.passthroughViews = #[btn];
popoverController.popoverContentSize = CGSizeMake(width, height);
popoverController.popoverLayoutMargins = UIEdgeInsetsMake(10, 10, 10, 10);
popoverController.wantsDefaultContentAppearance = YES;
[popoverController presentPopoverFromBarButtonItem:btn permittedArrowDirections:WYPopoverArrowDirectionAny animated:YES];
}
And this is the didSelctRow method (SearchViewController.m) :
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main"bundle:nil];
PostReaderViewController *postReaderView =
(PostReaderViewController *)
[storyboard instantiateViewControllerWithIdentifier:#"postReader"];
postReaderView.thePost = [_postsArray objectAtIndex:indexPath.row];
// if ([self.parentViewController.po .popoverController isPopoverVisible])
// [popoverController dismissPopoverAnimated:YES];
[self.navigationController pushViewController:postReaderView animated:YES];
}
To solve this issue I've passed the Navigation controller and popoverController from The parent View MainVC.h to child controller SearchViewController.h and then use those vars to dismiss my PopOvercontroller and push the new VC. Code snippet :
SearchView
#interface SearchViewController : UIViewController
#property (strong, nonatomic) WYPopoverController *parentPopoverVC;
#property (strong, nonatomic) UINavigationController *parentNavigationController;
#end
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
...
[self.parentPopoverVC dismissPopoverAnimated:YES];
[self.parentNavigationController pushViewController:postReaderView animated:YES];
}
MainVC.h
-(IBAction)showPopoverSearch:(id)sender{
SearchViewController *searchVC = [self.storyboard instantiateViewControllerWithIdentifier:#"searchView"];
[searchVC setParentPopoverVC:popoverController];
[searchVC setParentNavigationController:self.navigationController];
[popoverController presentPopoverFromBarButtonItem:btn permittedArrowDirections:WYPopoverArrowDirectionAny animated:YES];
}

navigationController = null in TableViewController's pushViewController method (popover)

First of all, sorry for my English.
I'm trying to do an app for iPad with a navigation controller which pushes view controllers when a button "Next" is selected. But I also want to have a popover, called from a button in the nav bar, that allows the user to "jump" from one view controller to another, pushing it with the tableView:didSelectRowAtIndexPath: and the pushViewController:animated: methods, but it's not working.
Summary:
Tab bar -> switches between FirstViewController and SecondViewController (works just fine)
Nav bar (button Next) -> switches between SecondViewController, FirstSlideController and SecondSlideController (it's also good)
Popover -> user selects SecondViewController, FirstSlideController or SecondSlideController (here's the problem!)
Codes:
AppDelegate
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[navigationController1, navigationController2];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
TableViewController(popover)'s didSelectRowAtIndexPath method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 0){
FirstSlideController *detailViewController = [[FirstSlideController alloc] initWithNibName:#"FirstSlideController" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES];
}
else if(indexPath.row == 1){
SecondSlideController *detailViewController = [[SecondSlideController alloc] initWithNibName:#"SecondSlideController" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES];
}
else{
SecondViewController *detailViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES];
}
}
SecondViewController (with delegate suggested by maros)
-(void) showPopover:(id) sender
{
TableViewController *PopoverView = [[TableViewController alloc] initWithNibName:#"TableViewController" bundle:nil];
self.popOver = [[UIPopoverController alloc] initWithContentViewController:PopoverView];
self.popOver.delegate = self;
[self.popOver presentPopoverFromBarButtonItem:self.navigationItem.leftBarButtonItem permittedArrowDirections: UIPopoverArrowDirectionUp animated: YES];
}
I tried to print self.navigationController and it says it's null. I'd appreciate any help.
Thank you.
Presnented UIPopoverController is not pushed on the navigation stack in the view controller from which it was presented. It is a separate view controller.
Therefore the navigationController inside the popover is nil.
What I would recommend you is to create a delegate MyNavigationPopoverDelegate (the class that creates a popover (PopoverController). Pass it's instance as an delegate to the TableViewController.
After users clicks on some button inside popover, call delegate's method to processes button clicks (myNavigationPopover:(UIPopoverController*)popover clickedButtonAtIndex:(NSInteger)buttonIndex).
Then maybe dismiss delegate?
and finally change navigation however you want! :)
#protocol MyNavigationPopoverDelegate
- (void) myNavigationPopover:(UIPopoverController*)popover clickedButtonAtIndex:(NSInteger)buttonIndex;
#end
#interface TableViewController : UITableVieController // your viewController in popover
... // your code
#property (nonatomic, weak) NSObject <MyNavigationPopoverDelegate> * delegate;
... // your code
#end
#implementation TableViewController
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.delegate myNavigationPopover:self clickedButtonAtIndex:indexPath.row];
}
...
#end
// defines that SecondViewController implements the delegate's method
#interface SecondViewController <MyNavigationPopoverDelegate> : UIViewController
// your code
#end
// code where you presenting popover
#implementation SecondViewController
// This is the method that is executed after your button press and it is responsible for presenting a popover
- (void) presentPopover{
...
myPopover.delegate = self; // setting the delegate
[myPopover presentPopoverFromXXX ...]; // however you present it
...
}
- (void) myNavigationPopover:(UIPopoverController*)popover clickedButtonAtIndex:(NSInteger)buttonIndex
{
UINavigationController *currentNavigationController = ; // get the navigation controller from the tab bar
if(buttonIndex == 0){
FirstSlideController *detailViewController = [[FirstSlideController alloc] initWithNibName:#"FirstSlideController" bundle:nil];
[currentNavigationController pushViewController:detailViewController animated:YES];
}
else if(buttonIndex == 1){
SecondSlideController *detailViewController = [[SecondSlideController alloc] initWithNibName:#"SecondSlideController" bundle:nil];
[currentNavigationController pushViewController:detailViewController animated:YES];
}
else{
SecondViewController *detailViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[currentNavigationController pushViewController:detailViewController animated:YES];
}
}
#end;

iOS table navcontroller not working

I have a table:
- (void)viewWillAppear:(BOOL)animated
{
self.searchBar.delegate=self;
self.searchResultTable.delegate=self;
self.searchResultTable.dataSource=self;
[self.searchResultTable setHidden:YES];
}
In my cells I insert text and when I click on cell I need go to another table but this not work:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TableViewController *tvc=[[TableViewController alloc]initWithStyle:UITableViewStyleGrouped];
[self.navigationController pushViewController:tvc animated:YES];
}
Assuming you are doing something like this to present this controller:
SearchViewController *searchViewController = [SearchViewController alloc] init];
[self presentModalViewController:searchViewController animated:YES];
then your view controller doesn't have a navigation controller
You need to change it to
SearchViewController *searchViewController = [SearchViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:searchViewController];
[self presentModalViewController:navController animated:YES];
then [self.navigationController pushViewController:tvc animated:YES]; will work

UITabBars and UITableViews. Keeping the Tab bar on screen when a table cell is clicked

I'm trying to get a GUI with a UITabBar and UITableViews set up.
I've got a UITabView that is programmatically created.
One of the Tabs displays a UITableView that is also programmatically created.
This UITableView then displays other views when didSelectRowAtIndexPath is called.
Unfortunately, when a table cell is clicked, my tab view goes away and the new table view is displayed.
What I can't get my head around is how to structure the views so that the tabBar stays on the screen.
Is it as simple as making the UITableViews shorter, or is there some window/view mojo that I'm missing?
Thanks
You should use a UITabBarController to display the UITabBar rather than doing it directly.
Then use a UITableViewController as the view controller for a given tab. Though I get the impression that you want to present descendent UITableViews when a row is selected. If this is the case, you ought to use a UINavigationController as the tab bar's view controller, and let it manage your UITableViewControllers.
Remember that on iOS you really need to use the view controller pattern - the frameworks take care of a lot of things for you under the hood.
Follow-up:
OK, the following straightforward implementation works just fine for me. Please ignore the many obvious issues with this code (beginning with the fact that I hacked it all together in the application delegate!); it's intended purely as a model for how your controllers should be glued together.
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate, UITableViewDelegate, UITableViewDataSource>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UINavigationController *navController;
#end
#implementation AppDelegate
#synthesize window = _window;
#synthesize navController = _navController;
- (void)dealloc
{
[_navController release];
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
UITableViewController *rootTVC = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
[[rootTVC tableView] setDelegate:self];
[[rootTVC tableView] setDataSource:self];
[rootTVC setTitle:#"Root Table"];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootTVC];
[rootTVC release];
[navController setTitle:#"My Table"];
UIViewController *anotherViewController = [[UIViewController alloc] init];
[anotherViewController setTitle:#"Not the table"];
UITabBarController *tbc = [[UITabBarController alloc] init];
[tbc setViewControllers:[NSArray arrayWithObjects:navController, anotherViewController, nil]];
[self setNavController:navController];
[navController release];
[anotherViewController release];
[[self window] setRootViewController:tbc];
[tbc release];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *reuseIdentifier = #"foo";
UITableViewCell *cell = [[tableView dequeueReusableCellWithIdentifier:reuseIdentifier] retain];
if (! cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
[[cell textLabel] setText:[NSString stringWithFormat:#"Row %d", [indexPath row]]];
return [cell autorelease];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewController *newTVC = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
[newTVC setTitle:[NSString stringWithFormat:#"Table %d", [indexPath row]]];
[[newTVC tableView] setDelegate:self];
[[newTVC tableView] setDataSource:self];
[[self navController] pushViewController:newTVC animated:YES];
}
#end
Use UITabBarController instead of UITabBar.

Resources