My question is quite simple but no way to find a simple exemple on internet.
I have a main view controller with buttons and i'd like to display an UIView created with IB when i click on a button, so for exemple:
-ViewController (.h/.m/.xib) is my main interface with a menu button
-MenuView (.h/.m/.xib) is my view that i would like to be displayed as a pop up window over my uiviewcontroller.
So how is it possible to control my MenuView from my viewController ? Is it possible to create it with IB or is it better to make it programmatically? Thank you very much for your help!
I don't know for a way to do this only with interface builder, but the IB can help you. I will do it creating another view controller which contains the view you want to be shown. On your already existing view controller you can create a method i.e:
- (void) show: (id) sender {
UIViewController *theNewController = [TheNewController alloc] initWithNibName: #"TheNewController" bundle:[NSBundle mainBundle]];
[self presentViewController:theNewController animated:YES completion:nil];
}
and link this method, using the IB, to the button actions.(right click on the button in IB, choose the event i.e Touch Down, drag'n drop to the File's Owner). But I recommend you also seeing view controller's apple documentation: http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html
Related
I am working on an application for IOS .I made a menu bar which contains buttons , menu bar should be on each page of application. I made many View controller scenes , I want the functionality like this : when i click on any button it should open a View controller scenes.
How to open a View controller scenes on click of button ? I don't want to add other existing View controller scenes on my current View controller scenes as a subview. I want to open other existing scenes independently.
You have to look into the following
Navigation controller
Tab bar controller custom implementation
Have you tried this, just replace YourViewController with the name of the .xib you would like to load.
YourViewController *yvc = [[YourViewController alloc] init];
[self presentViewController:yvc animated:YES completion:nil];
This goes inside your IBaction for the navigation button, fyi.
You need to look into UINavigationController or UITabBarController. There are other ways too. The navigation controller is the easiest. Apple documentation has everything you need.
1) Insert View :
ButtonView *buttonView = [[ButtonView alloc] initWithNibName:#"ButtonView" bundle:[NSBundle mainBundle]];
buttonView.view.frame = CGRectMake(yourFrame);
[self.view addSubview:buttonView.view];
2) Remove View :
[buttonView removeFromSuperview];
I am trying to remove two viewcontrollers (that have been added on top of each other) with one method. I have made the views in interfacebuilder. they all have their own .h and .m files to go with it.
Scenario I am in:
I have a main menu which has the view2 header file imported.
In a method I add the second view on top of the superview like so
view2ViewController * view2 = [[view2ViewController alloc] initWithNibName:#"view2ViewController" bundle:nil];
[self.view addSubview:view2.view];
then in view 2 I have added the view 3 header file so i can add view 3 as a subview ontop of view2. i have another method which is connected again to interface builder to a UIButton so upon button press a method gets called in view2 which adds view 3 on top in exactly the same way like so:
view3ViewController * view3 = [[view3ViewController alloc] initWithNibName:#"view3ViewController" bundle:nil];
[self.view addSubview:view3.view];
What im trying to solve:
I have a button in view 3 which should remove view 3.... and then it should also remove view 2 aswell so the main screen is visible.
How can this be achieved?
What I have so far:
[self.view removeFromSuperview];
This however only removes View 3... but leaves view 2 in place.
What needs to be modified so that i can remove view 2 as well??
Any help is appreciated.
I would normally do this as adding view2 and view3 as subviews of the main view. Then when the button actions are triggered, the adding and removing of subviews will be executed by the main view's view controller.
For a quick hack, I think you can try this in your button handler.
[self.view removeFromSuperview];
[self.view.superview removeFromSuperview];
Though I'm not sure if you should be doing it. :P
Is this what you need?
[[[self.view subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
I don't know what you're trying to do exactly, but I get the impression that pushing a new view controller is what you want. If you have a UINavigationController in your app, you'd just have to do a
[navigationController pushViewController:view2 aimated:YES]
To go back to the main menu when the button is pressed, you should define a delegate protocol that looks something like this::
#protocol View3ViewControllerDelegate
- (void)view3ControllerBackToMainMenuButtonPressed:(View3ViewController*)controller;
#end
This protocol is then implemented by the class that actually pushes the other view controllers. In the implementation, you'd pop all view controllers you don't want to be displayed anymore. To do this, you could use
[navigationController popToViewController:myMainMenuViewController animated:YES]
or if your main menu view controller is actually the root view controller:
[navigationController [navigationController popToRootViewControllerAnimated:YES]
That way only one class is responsible for pushing and popping all view controllers and handling that "Back to Main Menu" button. Using a custom protocol as described above is the recommended way to handle the popping of pushed view controllers in a scenario like this.
Hope that helps!
I am working on my first iPhone app and making good progress. But there is one thing I just don't understand.
When my app starts it displays a UIView with some functionality on it. This works fine. One of the buttons on that screen is supposed to load a new view (HistoryViewController) which contains a navigation controller.
The problem is… whenever HistoryViewController is loaded the app crashes because there is no view. It's true because in the xib-File I can't connect the File's Owner's view to anything:
http://www.freeimagehosting.net/image.php?1a3caa8b8d.png
I definitely have a lack of knowledge somewhere but after hours of research I have not been able to solve this problem.
Any hints?
Thank you!
Normally you would either:
click on that bottom line (History Table View Controller, "HTVC") and in the inspector window specify a NIB Name - which means you would first have to make a new NIB.
or
doubleclick that bottom line (HTVC), so the 320x480 preview window pops up, and then drag in a UIView from the library.
Using the first method, you tell the view controller to dynamically load the NIB as the view to connect, and using the second method you do this for the view controller using IB. The view you drag in will then show up as a child of that bottom line (HTVC).
edit to actually load the nib file you created, do this to push the view controller:
UIViewController *controller = [[UIViewController alloc] autorelease];
[controller initWithNibName:#"nibfilename" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
substituting UIViewController for your own view controller class (if needed) and nibfilename with the filename of the nib (minus the extension!)
It's hard to tell what your problem is exactly, but I'll offer some advice.
When creating a navigation controller (or tab controller for that matter) in interface builder, its easy to not understand what is really happening, so my suggestion drop interface builder for a second and lets build it in code.
In general I really dislike building either UI Navigation Controller or tab view controller in interface builder, I really just rather build the views themselves and create the UINavigationController in code.
You have a view which shows the HistoryTableViewController which you want to be contained in a UINavigationController so the code to do this is:
- (void) showHistory
{
HistoryViewController *historyVC = [[HistoryViewController alloc] init];
// If you create historyviewcontroller in nib
// HistoryViewController *histroyVC [[HistoryViewController alloc] initWithNibName:#"myNib" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootController:histroyViewController];
[self presentModalViewContoller:navController animated:YES];
}
This will create a nav controller showing your history view controller as the root view controller. Can't be easier.
first let me start by saying I'm pretty new to iPhone, so I apologise for my ignorance.
I've got a UITableView that I want to add new items to. When the add button is pressed, I want a modal screen to slide up where the user types in the text for the new item.
I've been reading from Apple's Table View Programming Guide for iPhone, and they have an example that supposedly does what I want:
- (void)addItem:sender {
// To add an item, display a modal view with a text field.
if (itemInputController == nil) {
itemInputController = [[ItemInputController alloc] init];
}
// Use a navigation controller to provide a customizable navigation bar with Cancel and Done buttons.
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:itemInputController];
[[self navigationController] presentModalViewController:navigationController animated:YES];
[navigationController release];
}
However they don't explain anywhere what itemInputController is. As far as I can determine it is supposed to give me a modal view with a single text field, and a navigation bar with Cancel and Save in it. Am I supposed to create this view myself in Interface Builder? Or is it a standard thing I need to import somehow? Can anyone help me decipher this, or alternatively show me another way to get this working?
ItemInputController will be a derivitave of UIViewController that you need to add to your project.
You'll need to create a new UIViewController subclass, then build the interface in IB - see here for a discussion on building UIViewControllers.
Here goes stupid question again :(
I am trying to create a sample app that loads UIViewController when the app loads and the view contains a button to load UINavigationViewController.
Now I created a project with "Window-based Application" and added "RootViewController" in the project with .m, .h, and .xib.
Next I added a view and a button in the "RootViewController.xib" file and it runs ok. After that, I added "UIViewController subclass" file naming "NavViewController" with .h, .m and .xib files.
Also I added - (IBAction)buttonPressed:(id)sender function in the "RootViewController" classes to load NavigationViewController.
Here is the code of the "buttonPressed:".
- (IBAction)buttonPressed:(id)sender {
NavViewController *navViewController = [[NavViewController alloc] initWithNibName:#"NavViewController" bundle:nil];
self.navController = navViewController;
[self.view insertSubview:navViewController.view atIndex:0];
[navViewController release];
}
When I "build and go," it runs fine initially until I press the button. When I press button, program terminates it.
What am I doing wrong? Please help...
Thank you.
What are you doing wrong? Designing your app in a non-standard way - you are not supposed to be able to do this - the NavigationController is in charge!
Why would you have a button that then adds a navigation controller? - it goes against the user interface guidelines. I found it hard to get to grips with the interface guidelines to begin with but you really must because it will make your app so much more usable.
If you need a navigation controller then add it to the view to begin with - or create a new view with the navigation controller. Honestly try it out and you will feel the user interface feels much better.
If you really want a button that adds a navigation controller to the window then do the following:
Keep a reference to the AppDelegate in your code
Use this reference and pass in your current view controller to a method called reloadMainViewWithNavBar:(UNViewController*) viewController
This new method should remove the old mainViewController and create a NavigationController
using your viewController as the root view conroller
add the navigation controller view to window view
The navController has no view set in the nib. A UINavigationcontroller needs a root view. In the nib, connect the navigation controller to a another view controller.
In addition to the fix mentioned above, what you really want to do is create the first view controller contained in the navigation controller - but hide the navigation bar until the button press causes it to unhide. You cannot easily add a navigation controller to a view you are in.