I've set up a navController, which appears after tapping a button. However, if I tap the button I get the error of: "Warning: Attempt to present <UINavigationController>: 0xab5d9d0 on <MyApp: 0xadaa320> whose view is not in the window hierarchy!"
Does anyone know how to solve this? I also tried something on Stackoverflow but it wasn't my solution.
Here my code for opening the navigationcontroller:
I dont know if somebody know this photogallery but if you don't, here is the project.
My code (MyApp.m):
#import MyApp.h
...
//some stuff
- (void)launchGalleryView
{
MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
// Set browser options.
browser.wantsFullScreenLayout = YES;
browser.displayActionButton = NO;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:browser];
NSMutableArray *photos = [[NSMutableArray alloc] init];
MWPhoto *photo;
photo = [MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:#"callculator" ofType:#"jpg"]];
photo.caption = #"The calculator is soo beateful...";
[photos addObject:photo];
self.photos = photos;
[self presentModalViewController:navController animated:NO];
}
Thanks in advance.
Edit:
it is in the recources and in the compile sources but in the resources you can see that it is red (the storyboard). Maybe it's caused by this?
The Second controller .h:
#class MyApp;
#interface Second : UIViewController <MWPhotoBrowserDelegate> {
}
#property (nonatomic, retain) MyApp* vC;
#end
The Secnond controller .m:
#import "Second.h"
#import "MyApp.h"
#interface Second ()
#end
#implementation Second
#synthesize vC;
//some stuff in here
//the action
- (IBAction)dothis:(id)sender {
NSLog(#"launch the navcontroller");
[self.vC launchGalleryView];
}
MyApp.h:
#import "Second.h"
#interface myApp : UIViewController <MWPhotoBrowserDelegate> {
}
-(void)launchGalleryView;
NSArray *_photos;
NEW EDIT:
I found that I have to call the method "launchGalleryView" in the viewDidAppear but how can I do this without calling the navcontroller everytime the view loads? Does anyone know how to do this?
i checked your project.. wasn't able to sort out the proper issue..
but i tried a hack and it worked..
replace this line with
[self presentModalViewController:navController animated:YES];
this
[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentModalViewController:navController animated:YES];
Related
May somebody let me know:
How to navigate from one view to another view in single viewcontroller in iphone sdk? I have an application in which i want to push to next view and pop to previous view in a single View controller. How this functionality can be achieved?
Thanks in advance
You can add and remove your secondview.Like this to navigate
-(IBAction)navigate:(id)sender
{
[self.view addSubView:secondView];
}
and this one to po to first view
-(IBAction)popToFirstView:(id)sender
{
[secondView removeFromSuperView];
}
Note:- You can use animations for adding and removing view, if you want to give animated effects.
If you need a single view controller you can try using a UIScrollView, that contain the 2 view, and a button to scroll from a view to another... but if i can give a tip, it's better using 2 view controller and a navigation view controller
You need to use UINavigation Controller to get push and pop.
do this way
In AppDelegate.h file
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
//this will be your first screen
#property (strong, nonatomic) FavViewController *favViewController;
#end
In A*ppDelegate.m* file
#import "AppDelegate.h"
#import "FavViewController.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize favViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//initialize view controller
favViewController = [[FavViewController alloc] initWithNibName:#"FavViewController" bundle:nil];
//add navigation controller
UINavigationController *favouriteView = [[UINavigationController alloc] initWithRootViewController:favViewController];
//add controller to witndow
self.window.rootViewController = favouriteView;
[self.window makeKeyAndVisible];
return YES;
}
#end
Now come to the UIViewController, where you want to load/push to new controller
to load/push to new controller use this code
//this will be next screen
DetailsViewController *detailsViewController = [[DetailsViewController alloc ]init];
[self.navigationController pushViewController:detailsViewController animated:YES];
to go back or pop controller use this
//now it will send back to parent screen
[self.navigationController popViewControllerAnimated:YES];
I have figured out the solution for this problem and here is the solution as below:-
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
IBOutlet UIButton *backbtn;
IBOutlet UIButton *nxtBtn;
IBOutlet UILabel *label;
}
-(IBAction)nxt:(id)sender;
-(IBAction)ba:(id)sender;
#end
#import "ViewController.h"
#interface ViewController ()
#end
int count=0;
#implementation ViewController
- (void)viewDidLoad
{
label.text=[[NSUserDefaults standardUserDefaults] objectForKey:#"NavigtionNumber"];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
-(IBAction)nxt:(id)sender
{
count++;
label.text=[NSString stringWithFormat:#" navigation time %d",count];
[[NSUserDefaults standardUserDefaults] setObject:label.text forKey:#"NavigtionNumber"];
[[NSUserDefaults standardUserDefaults] synchronize];
ViewController *vc=[[ViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
}
-(IBAction)ba:(id)sender
{
count--;
label.text=[NSString stringWithFormat:#" navigation time %d",count];
[[NSUserDefaults standardUserDefaults] setObject:label.text forKey:#"NavigtionNumber"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self.navigationController popViewControllerAnimated:YES];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
Hope this will help others in future.
Thanks.
I have a project with one View. I'm drawing everything on it programically. So when i want to add another View (screen) to my project i create a class inherits from the UIViewController class and implements method
- (void)viewDidLoad
And then i want to load this View from my original View, and i do this:
In ViewController.h
#import <UIKit/UIKit.h>
#import "TestViewControllerClass.h"
#interface ViewController : UIViewController <UITableViewDataSource> {
}
#property (strong,nonatomic) TestViewControllerClass *testView;
#end
In ViewController.m
#implementation ViewController
#synthesize testView;
- (void)viewDidLoad
{
[super viewDidLoad];
testView = [[TestViewControllerClass alloc] init];
[self.view addSubview:testView]; //crash here
}
And then in my TestViewControllerClass.h
#import <UIKit/UIKit.h>
#interface TestViewControllerClass : UIViewController
#end
And TestViewControllerClass.m
- (void)viewDidLoad
{
[super viewDidLoad];
}
To check if method wiewDidLoad will be executed i put there a breakpoint, but nothing happend. In fact, my app crash (I put comment at code where).
When crashes i receive: -[TestViewControllerClass superview]: unrecognized selector sent to instance 0x683aca0
Replace
[self.view addSubview:testView]; //crash here
with
[self.view addSubview:testView.view];
Use this code...
[self.view addSubview:testView.view];
Hope,this will help you...
What you're doing is settings your ViewController as the view, not the real view
testView = [[TestViewControllerClass alloc] init];
[self.view addSubview:testView]; //crash here
this will obviously crash. Assuming you have a view variable declared in your header file called view, use
testView = [[TestViewControllerClass alloc] init];
[self.view addSubview:testView.view];
You can load two types for uiviewcontroller loading below:
[self.view addSubview:testView.view];
(or)
use presentmodalviewcontroller below code:
testView *popupView = [[testView alloc] initWithNibName:#"testView" bundle:nil];
popupView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:popupView animated:YES];
dimiss modal view cntroller below code:
[self dismissModalViewControllerAnimated:YES];
thanks..!
EDIT: Added Source Project
--> I uploaded a sample project that clearly shows my dilemma which can be found here <--
I created a Split View-based Application. I then added a second UINavigationController to the DetailViewController inside the MainWindow.xib.
I then pop a new UIViewController Subclasses when a toolbar item is clicked. I use the following code to conduct the pop:
DocumentDetailVC *DetailViewController = [[DocumentDetailVC alloc] initWithNibName:#"DocumentDetailVC" bundle:[NSBundle mainBundle]];
[detailViewController.detailNavController pushViewController:DetailViewController animated:YES];
DocumentsVC *RRequestViewController = [[DocumentsVC alloc] initWithNibName:#"DocumentsVC" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:RRequestViewController animated:YES];
This works. The issue I am having is how do I pass information or function calls from the Main side of the Split View to the Detail side of the Split view?
If I present the UIViewController via the following method:
DocumentDetailVC *RRequestViewController = [[DocumentDetailVC alloc] initWithNibName:#"DocumentDetailVC" bundle:[NSBundle mainBundle]];
RRequestViewController.delegate=self;
RRequestViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
[RRequestViewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:RRequestViewController animated:YES];
[RRequestViewController release];
RRequestViewController = nil;
I am able to complete a reach back through a protocol as intended.
DocumentDetailVC, when loaded via the pushViewController, the hierarchy is as follows:
NSLog([NSString stringWithFormat:#"%#",self]);
//self = <DocumentDetailVC: 0x4e0d960>
NSLog([NSString stringWithFormat:#"%#",self.parentViewController]);
//self.parentViewController = <UINavigationController: 0x4e0ce30>
NSLog([NSString stringWithFormat:#"%#",self.parentViewController.parentViewController]);
//self.parentViewController.parentViewController = <UISplitViewController: 0x4e0d0f0>
Thank you for your assistance. This problem is consuming my life!
--> I uploaded a sample project that clearly shows my dilemma which can be found here <--
Ok, I know am very late for answer but this answer is, I think, perfect and working. Try it. Open your RootViewController.h, on the top write this line:
#import "Left.h"
#import "Right.h"
in #interface RootViewController write this lines
Left *lefty;
Right *righty;
after that declare property as,
#property (nonatomic, retain) Left *lefty;
#property (nonatomic, retain) Right *righty;
go to ROOTVIEWCONTROLLER.M file synthesize as,
#synthesize lefty;
#synthesize righty;
after that in RootViewController.m just replace your function with this one
- (IBAction)myBtnPushed{
NSLog(#"myBtnPushed");
lefty = [[Left alloc] initWithNibName:#"Left" bundle:[NSBundle mainBundle]];
righty = [[Right alloc] initWithNibName:#"Right" bundle:[NSBundle mainBundle]];
lefty.delegate=righty;
[self.navigationController pushViewController:lefty animated:YES];
[detailViewController.navigationController pushViewController:righty animated:YES];
}
in delloac write this,
[lefty release];
lefty = nil;
[righty release];
righty = nil;
It's done, run your app. I tested, it's done.
I have a small app that from the home screen has a few buttons, when clicked these buttons load in a new view, this all works fine:
- (IBAction)showMaps:(id)sender {
MapViewController *viewcontroller = [[MapViewController alloc] initWithNibName:#"MapView" bundle:nil];
[[self view] addSubview:viewcontroller.view];
[viewcontroller release];
}
The problem comes when the MapView is loaded I have created a new IBAction in the MapViewController.h file:
- (IBAction)showHome:(id)sender;
And also this action within the MapViewController.m file:
- (IBAction)showHome:(id)sender {
[self.view removeFromSuperview];
}
But no joy, bit of a newbie to this so any help more than welcome!
Your showMaps: method creates a view controller, but does not retain it. You will need to retain ownership of that viewController if you want it to stick around. I would suggest adding a property on your main view controller - the one that contains the showMaps: method. Example code below:
MainViewController.h
#interface MainViewController : UIViewController {
MapViewController * mapViewController;
}
#property (nonatomic, retain) MapViewController * mapViewController;
- (IBAction)showMaps:(id)sender;
#end
MainViewController.m
#implementation MainViewController
#synthesize mapViewController;
- (void)dealloc {
[mapViewController release];
[super dealloc];
}
- (IBAction)showMaps:(id)sender {
self.mapViewController = [[[MapViewController alloc]
initWithNibName:#"MapView"
bundle:nil] autorelease];
[[self view] addSubview:mapViewController.view];
}
#end
I'm using two UIViewController in Application Delegate and navigating to UIViewController using presentmodalviewcontroller. But Problem is that presentmodalviewcontroller works for first time UIViewController and when i want to navigate to second UIViewController using presentmodalviewcontroller then its showing first UIViewController.
The following is the code:-
-(void)removeTabBar:(NSString *)str
{
HelpViewController *hvc =[[HelpViewController alloc] initWithNibName:#"HelpViewController" bundle:[NSBundle mainBundle]];
VideoPlaylistViewController *vpvc =[[VideoPlaylistViewController alloc] initWithNibName:#"VideoPlaylistViewController" bundle:[NSBundle mainBundle]];
if ([str isEqualToString:#"Help"])
{
[tabBarController.view removeFromSuperview];
[vpvc dismissModalViewControllerAnimated:YES];
[viewController presentModalViewController:hvc animated:YES];
[hvc release];
}
if ([str isEqualToString:#"VideoPlaylist"])
{
[hvc dismissModalViewControllerAnimated:YES];
[viewController presentModalViewController:vpvc animated:YES];
[vpvc release];
}
}
Can Somebody help me in solving the problem?
You're making a new hvc and vpvc each time you run this function.
The first time through, I assume you call removeTabBar:#"Help", it makes a hvc and vpvc and then shows the correct one.
The second time you call it removeTabBar:#"VideoPlayList", you are making a new hvc and vpvc. This means that when you call hvc dismissModalViewController:YES]; you're not removing the one you added before, you're removing the new one that you just made which isn't being displayed at all!
To solve this you need to make your two controllers as properties in your app delegate and create them in the applicationDidFinishLaunching method.
Add these into your app delegate's .h file:
#class MyAppDelegate {
HelpViewController *hvc;
VideoPlaylistViewController *vpvc;
}
#property (nonatomic, retain) HelpViewController *hvc;
#property (nonatomic, retain) VideoPlaylistViewController *vpvc;
#end
and in your app delegate's .m file :
- (void)applicationDidFinishLaunching:(UIApplication *)application {
...
self.hvc = [[[HelpViewController alloc] initWithNibName:#"HelpViewController" bundle:nil] autorelease];
self.vpvc = [[[VideoPlaylistViewController alloc] initWithNibName:#"VideoPlaylistViewController" bundle:nil] autorelease];
...
}
and remove the first two lines in removeTabBar