In the app I am trying to build, I am trying to use the UIImageController but am running into a bit of a problem. I am not doing this in the ViewController class... Here's the code:
(IBAction) uploadBtn {
ipc = [[UIImagePickerController alloc] init];
ipc.delegate = self;
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:ipc animated:YES];'
Its giving me a warning with the last line, saying:
Method'- presentModalViewController:animated' not found
I am sure that if I used the ViewController class this would work but is there anything I can do/import that will let me do this in just a UIView class?
Again the app builds, but when I try to click the button it crashes
Thanks in advance!
You're crashing because only UIViewController has that method. You can only present one view controller off of another one.
Have a look at get UIView's viewcontroller (iphone). I think you should rethink your design if you need to access view controller from a UIView.
You could present it from the view's parent view controller:
[[ipc parentViewController] presentModalViewController:yourController animated:yourBool];
Related
I have got a IIViewDeckController set up to replicate the facebook app funcionaly
ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewController"];
EditSettingsViewController *evc = [self.storyboard instantiateViewControllerWithIdentifier:#"EditSettingsViewController"];
IIViewDeckController* deckController = [[IIViewDeckController alloc] initWithCenterViewController:vc leftViewController:evc rightViewController:nil];
In order to switch, I have got a tableview that calls different methods.
When I switch the center controller to a new controller it crashes:
MyTableViewController *mvc = [self.storyboard instantiateViewControllerWithIdentifier:#"MyTableViewController"];
[self.viewDeckController setCenterController:mvc];
But when I do exactly the same to the initial center viewcontroller it doesn't crash.
I have also noticed, that when I reset all the content in the simulator I am able to switch once to the new controller without crashing.
It doesn't show any warning. Just the following:
Solved my issue.
In the initial Controller I was using the viewdeckDelegate and implementing a few methods.
When switching to the new viewcontroller, I wasn't defining the delegate when those methods where being called.
I'm having a little predicament switching between views, here.
Alright, so, I have this view controller class in my iPhone project called "BaseViewController," which is the default, which has a button called "GoToNextView." I added another ViewController to the storyboard called "NextViewController," and then I created another custom view controller class called "NextViewController." Under the inspector window for NextViewController on the storyboard I changed its custom class to "NextViewController;" I'm assuming everything should be hooked up, now. When I click on the "GoToNextView" button, though, the application stops with a SIGABRT message.
Here's the code for my button click action in the BaseViewController class.
- (IBAction)Transition_Next:(id)sender
{
nextViewController = [[NextViewController alloc]
initWithNibName:#"SecondView"
bundle:[NSBundle mainBundle]];
[self.view addSubview:nextViewController.view];
}
What might I be doing wrong, here?
Thanks in advance...
You can use:
- (IBAction)Transition_Next:(id)sender
nextViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:nextViewController animated:YES];
}
Instead of your code and it should work! Hope that helps!
You can use [self.navigationController pushViewController:nextViewController animated:NO]; or if you don't have a navigation controller, you may use
[self presentModalViewController:nextViewController animated:NO]; instead of [self.view addSubview:nextViewController.view];
a quick question,
I have been dabbling with XCode off late and am trying to understand the View Controller, while I get the nitty gritty of it, one thing I fail to see is where the View Controller class object is instantiated. It is, in essence a class and hence has to have an object instantiated to be able to send messages to it.
It's kind of left me scratching my head.
Thanks much!
It is instantiated whenever it needs to be displayed. By you.
For example, if you wanted to display a new view on the navigation stack on the press of a button?
-(IBAction)buttonClicked:(id)sender{
/* Create VC here */
YourViewController *controller = [[YourViewController alloc]initWithNibName:#"ViewName"];
/* Push */
[self.navigationController pushViewController:controller animated:YES];
/* Let go since you don't have control over it anymore. */
[controller release];
}
I believe it is generally better to do this instead of holding an instance in memory in most situations, to prevent too much memory usage.
Now (assuming iOS5 is out of NDA now that most of it has been announced today), you can use Storyboarding in XCode that will handle all this for you.
The view controller is instantiated in it's init function designated initializer being the following:
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
if( (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) )
{
// Custom initialization
}
return self;
}
As you will perhaps have noted in some of Apples examples or any other source code you've looked through you might have seen a line of code similar to
MyViewController* viewController = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
This is where/when the view controller gets instantiated. You'll notice that a view controller has a member object of type UIView called view it is this that gets added to the window or view that this view is to be apart of.
The view controller is created to handle messages pertaining to this view. It's all spelled out here.
I have the following Objective-C code:
MainMenu *main= [[MainMenu alloc] initWithNibName:nil bundle:nil];
[[self navigationController]pushViewController:main animated:YES];
NSLog(#"hello");
I have a class called 'MainMenu' with a corresponding header file and xib file. No matter what I do, it simply won't show. I have confirmed that the code gets to the above, because of the NSLog('hello').
I've been pulling my hair out for hours now and I simply cannot get to the bottom of it.
I hope someone can help,
Edit - still having problems...
Here are some screenshots of my project setup:
Ok, so I tried this:
MainMenu *main= [[MainMenu alloc] initWithNibName:nil bundle:nil];
[[self navigationController]pushViewController:main animated:YES];
[self.view addSubview:main.view];
But it still doesn't work...
Many thanks in advance,
The fact that initWithNibName is nil should not be the problem because if it is given nil it looks for a nib with the exact name of the class.
Two things:
1) Make sure you have run a clean recently and make sure that file is correctly being loaded.
2) Make sure navigationController is not nil, if it is then you need to make sure you make a navigation controller if you are not intending on using a navigation controller, consider using:
- (void)presentModalViewController:(UIViewController *)modalViewController
animated:(BOOL)animated
Why are you setting the nibName to nil? If the name of the nib is MainMenu, then you want:
MainMenu *main= [[MainMenu alloc] initWithNibName:#"MainMenu" bundle:nil];
[self.navigationController pushViewController:main animated:YES];
[main release];
Are you sure that you have a UINavigationController in order to push a new view?
Hope that Helps!
Dont pull your hair just look at your code closely: You have nil in your initWithNibName. Whats MainMenu is it a viewController or what ? and place your correct nib for your to get Hello.
Updated as asked :
MainMenu *main= (MainMenu *)[MainMenu alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:main];
[self.navigationController presentModalViewController:nav animated:YES];
NSLog(#"hello");
I guess the issue is that you do not have a navigation controller set up .Try to present the view controller by
[self presentModalViewController:main animated:YES];
Verify that the object owner is in fact MainMenu and the view is connected.
In the MainMenu NIB, select File's Owner and the click on the Identity Inspector. Class should match your VC class name.
Then select the main View in your NIB and click on the Connections Inspector. The view outlet should be connected to your File's Owner.
If those are both set correctly, then post some more surrounding code. Notable point, if those are both set,
MainMenu * main= [[MainMenu alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:main animated:YES];
will load the correct NIB and display it.
What are you seeing? Another point if MainMenu is a subclass of some other VC with a NIB you will have to change the base class' init to override the default behavior, for example:
self = [super initWithNibName:nibNameOrNil == nil ? #"BaseViewController" : nibNameOrNil bundle:nibBundleOrNil]
But in that case you would have to specific the NIB that will override the base view controller's.
Post more code and let us know what you are seeing when you run.
I'm having an incredibly frustrating problem that appears to be a bug, but I have a very hard time believing no one else has come across this. My application's root view controller is a UITabBarController, where each tab is a UINavigationController. Everything works great.
Now I've come to a place where I want to edit the stack, so I rearrange the viewControllers of the current navigation controller and then do:
[self.navigationController setViewControllers:newViewControllers animated:YES];
The stack is correctly popped/pushed to the top view controller, but the navigation bar does not update to the current view controller and seems to remain exactly as it did with the viewController before the pop. If I do:
[self.navigationController popToViewController:someViewController animated:YES];
Everything works perfectly. Has anyone ever come across this before? Is there a workaround? Something I'm doing wrong?
I faced the same problem, it seems that Apple haven't corrected that bug and as a result the selected answer of this thread appears to be incorrect.
I managed to correct this problem using this bug report as in the comment of Anurag combined with the comment of Scott Pfeil.
Here is the code :
navController.navigationBarHidden = YES;
NSArray* viewControllers = navController.viewControllers;
UIViewController* currentController = [viewControllers objectAtIndex:viewControllers.count-1];
NSArray *controllers = [NSArray arrayWithObjects: viewController , currentController , nil];
[navController setViewControllers:controllers animated:NO];
navController.navigationBarHidden = NO;
I call this code in the viewDidLoad of the currentController and what I did is replace the previous controllers with only viewController.
Hope this helps.
Apple appears to have fixed this in the newest SDK
Two equally ugly work arounds.
First,
If:
[self.navigationController popToViewController:someViewController animated:YES];
Works well, try pushing an extra viewcontroller on the stack and then call:
[self.navigationController popToViewController:someViewController animated:NO];
Meaning you should get to the vc you want without any animation.
Second,
Before setting the stack, set the leftButtonBarItem = nil;
Effectively removing the old view controller's button. In fact if the title is wrong, change that too.
Neither is exactly clean but may get you the desired results.
You can also set your root view controller as the UINavigationController's delegate like:
#interface YourViewController : UIViewController <UINavigationControllerDelegate> {
and then in the didShowViewController delegate method you manually set the available view controllers:
-(void)navigationController:(UINavigationController*)navigationController didShowViewController:(UIViewController*)viewController animated:(BOOL)animated {
[[viewController navigationController] setViewControllers:[[viewController navigationController] viewControllers]];
}
Let me know if this works in your environment!
I'm still facing this issue in the Xcode 9.4.1 & iOS 11.4 .
The easiest way is to call loadViewIfNeeded() for all previous view controllers in the navigation stack:
let menuViewController = ...
menuViewController.loadViewIfNeeded()
let submenuViewController = ...
navigationController.setViewControllers([menuViewController, submenuViewController], animated: true)
[self.navigationController setViewControllers:newViewControllers animated:NO];
this may help you.