I have one base view controller "contentViewController" with one button
the action on button is
(IBAction) goBack:(id)sender
.h file
#interface ContentView : UIViewController {
}
#property(nonatomic,retain) UIViewController *display;
-(IBAction) goBack:(id) sender;
.m file
#synthesize display;
-(IBAction) goBack:(id)sender{
UIViewController *view= display;
[display release];
[self presentModalViewController:view animated:YES];
}
and there are some other view controllers already exist each view controller contain on button to show content on the contentViewController.. here is one class example:
.h file
#interface Info : UIViewController {
}
-(IBAction) viewHealthInfoContent:(id) sender;
.m file
-(IBAction) viewHealthInfoContent:(id)sender{
ContentView *cv=[ContentView alloc];
[cv setDisplay:self];
[self presentModalViewController:cv animated:YES];
[cv release];
}
the case is, each time i show content from one view controller i need to go back to it. using that one goBack button on the contentViewController but when i click the go back button it doesn't do any think !!! any help
When you want to go back your previous viewController, use dismissModalViewControllerAnimated:: method.
According source :
Dismisses the modal view controller that was presented by the receiver.
In iOS, you can display views modally by presenting the controller for the modal view from your current view controller. When you present a view modally using the presentModalViewController:animated: method, the view controller animates the appearance of the view using the technique you specify. (You can specify the desired technique by setting the modalTransitionStyle property.) At the same time, the method creates a parent-child relationship between the current view controller and the modal view controller.
Source
Related
- (IBAction)loginButtonAction:(id)sender
{
[self.navigationController popToViewController:
[self.navigationController.viewControllers
objectAtIndex:self.navigationController.viewControllers.count -2]
animated:YES];
}
This is my logout code in 2nd view controller, i want to remove text
in login screen of user name and password when clicked on Logout
button.. Please can some one help me?
You can use delegate or NSNotification for this.
Delegate.
Lets you have two View Controller.. One is ViewController and other NextViewController and You want to logout from NextViewController and clear the text in ViewController.
Steps: In NextViewController.h
#import <UIKit/UIKit.h>
#protocol clearTextField <NSObject>
-(void)clearTextFieldInPreviousController: (NSString *)string;
#end
#interface NextViewController : UIViewController
#property (assign,nonatomic) id delegate;
#end
In nextViewController.m
- (IBAction)loginButtonAction:(id)sender
{
[_delegate clearTextFieldInPreviousController:#""];
[self.navigationController popToViewController:
[self.navigationController.viewControllers
objectAtIndex:self.navigationController.viewControllers.count -2]
animated:YES];
}
And In Your ViewController.m
-(void)clearTextFieldInPreviousController: (NSString *)string{
NSLog(#"Fired");
self.label.text =#"";
}
// Note before going to next controller. You will have to set the delegate.
– (IBAction)goNextButtonAction:(id)sender {
NextViewController *acontollerobject=[self.storyboard instantiateViewControllerWithIdentifier:#"NVCSID"];
acontollerobject.delegate=self; // protocol listener
[self.navigationController pushViewController:acontollerobject animated:YES];
}
you can visit this link for Demo Example
There are a number of ways to achieve this.
Use delegation to pass message to 1st viewcontroller about the logout event from 2nd view controller and reset views in the passed message implementation.
I will suggest you to make 2nd view controller as rootViewController and not to keep the login view as part of navigation stack. You can show the login controller as a modal viewController over the 2nd View controller.
I have a question about using NSViewController and switching between views. I have a Cocoa application where I have a window. The idea with the window is that it will display a number of views one by one where each view is stored in a separate XIB file. Each view has a corresponding NSViewController. I have made a minimal example of what I'm doing where only the first view is loaded.
#interface MyWindowController : NSWindowController {
NSViewController *currentViewController;
}
#property (assign) IBOutlet NSView *targetView;
#end
#implementation MyWindowController
#synthesize targetView;
- (id)init
{
return [super initWithWindowNibName:#"MyWindow"];
}
- (void)dealloc
{
[currentViewController release];
[super dealloc];
}
- (void)windowDidLoad
{
[super windowDidLoad];
currentViewController = [[NSViewController alloc] initWithNibName:#"FirstView" bundle:nil];
[self.targetView addSubview:currentViewController.view];
[currentViewController.view setFrame:targetView.bounds];
}
#end
When the window is loaded the view from FirstView.xib is also loaded and the view is displayed in the window. In this case the loaded view only has a text field and I would like the text field to be highlighted so that input can be written to it directly without the user having to click on it but I can't figure out how to do that. Is it possible to have the text field selected when the view is loaded?
After reading the documentation I have found that I probably want to set the window's initialFirstResponder to the text field, but I can't find how to do that when the text field is in a different XIB file than the window.
Whenever you add/replace a subview and want it to be the first responder, have your window controller make the view the first responder of the window managed by the window controller:
[[self window] makeFirstResponder:currentViewController.view];
You’ll want to do this in both -windowDidLoad and whichever other methods add/replace subviews.
In my UISplitViewController the master controller is a UINavigationController.
When in portrait mode I would like to keep the navigation controller visible as long as the user navigates upwards (using the back button). As soon as an item from the table view of the navigation controller is selected, I want to dismiss the popover.
How can I achieve this? How can my UITableViewController know if it is inside a popover, and if yes, dismiss itself?
Make your main view controller a UISplitViewControllerDelegate (if it isn't already) and wire it up to the UISplitViewController's delegate outlet.
Create a UIPopoverController variable in your main view controller:
// MyViewController.h
#interface MyViewController : UIViewController <UISplitViewControllerDelegate> {
UIPopoverController *popoverController;
}
#property (retain, nonatomic) UIPopoverController *popoverController;
// MyViewController.m
#synthesize popoverController;
Implement the following UISplitViewControllerDelegate methods:
// Called when rotating to portrait
- (void)splitViewController:(UISplitViewController*)svc
willHideViewController:(UIViewController *)aViewController
withBarButtonItem:(UIBarButtonItem*)barButtonItem
forPopoverController:(UIPopoverController*)pc {
// Popover controller is visible in portrait
self.popoverController = pc;
}
// Called when rotating to landscape
- (void)splitViewController:(UISplitViewController*)svc
willShowViewController:(UIViewController *)aViewController
invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {
// No popover controller in landscape view
self.popoverController = nil;
}
In your own handler in the main view controller (the one that gets called when a naviation item is selected in the table view):
- (void)navigationControllerSelectedItem:(id)item {
// If a popover controller is visible, hide it
if (popoverController) {
[popoverController dismissPopoverAnimated:YES];
}
}
And don't forget to release that variable:
- (void)dealloc {
self.popoverController = nil;
[super dealloc];
}
Hope that helps!
The standard iPad sample for SplitViewController in iOS5 does about the same as the elaborate answer, but the popoverController is called masterPopoverController.
And creating the property iOS5 style as _popoverController does not work, because there as already an ivar with that name in UIViewController.h.
The IOS 6.0 SplitView template has this built in. The detail view tracks the orientation and the MasterViewController popover.
Simply set the detailItem and the popover disappears if appropriate. There is even a check if you are using the same detaiItem so no page setup and refresh work gets done.
self.detailViewController.detailItem = self.detailViewController.detailItem;
I have subclassed UIView and created a NIB that controls the main logic for my application.
Hoping the view will scale nicely, I want to use it for both the iPhone and iPad versions of the app.
On the iPhone the view will cover the full screen. On the iPad the view will cover only part of the screen.
I have read that you shouldn't use UIViewControllers to control only part of the screen. So, I am trying to embed the custom UIView in the main UIViewController's view using IB.
How can this be done?
After a lot of trial and error I found a solution based on an approach explained in the following question, answered by Brian Webster.
The solution was originally suggested for a Cocoa environment. I hope it is valid in an iOS environment as well.
Create the main view controller with a NIB-file. In the NIB, the File's Owner should correspond to the class of your main view controller.
Create a custom view controller with a NIB-file. In this NIB, the File's Owner should correspond to the class of your custom view controller.
Create a custom view controller property in your main view controller class.
Create an UIView property in the main view controller class. It will hold your custom view controller's view. Define it as an IBOutlet, so it can be linked in the NIB.
Drop a UIView in your main view controller's NIB. Link it to the main view controller's view IBOutlet. It will be used as a placeholder for the custom view.
In the main view controller's viewDidLoad method, load the custom view controllers NIB, determine the custom view's frame size and copy the view in the main view controller's view.
Here is some code:
MainViewController.h
#interface MainViewController : UIViewController {
CustomViewController *customViewController;
UIView *customView;
}
#property (nonatomic, retain) CustomViewController *customViewController;
#property (nonatomic, retain) IBOutlet UIView *customView;
#end
MainViewController.m
- (void)viewDidLoad {
CustomViewController *controller = [[CustomViewController alloc] initWithNibName:#"CustomViewController" bundle:nil];
self.customViewController = controller;
[controller release];
customViewController.view.frame = customView.frame;
customViewController.view.autoresizingMask = customView.autoresizingMask;
[customView removeFromSuperview];
[self.view addSubview:customViewController.view];
self.customView = customViewController.view;
[super viewDidLoad];
}
Add an IBOutlet propertyfor your custom UIView to the UIViewController, and additional outlets for any subviews you wish to access.
Go to Interface Builder, select the "File's Owner" object in your NIB and in the Inspector go the rightmost tab set its class to match your UIViewController's class.
Connect the IBOutlet from step one on the "File's Owner" to your custom UIView.
In XCode, when you need to load your view, do something like this:
--
[[NSBundle mainBundle] loadNibNamed:#"MyNib" owner:self options:0];
self.myCustomView.frame=self.view.bounds; // make view fill screen - customize as necessary
[self.view addSubview:self.myCustomView];
When you load the NIB, the outlet(s) you set up in step 1 will be populated with the objects loaded from your NIB.
I have a modal view which gets the user to select some data to add to a table. When the user presses a save button, the modal view should disappear and send the required data back to the view controller that presented the modal view for further processing. To achieve this, I have set up a protocol. The protocol method in the original view controller does not get called. My code is below, what am I doing wrong?
The header file (modal view controller):
#protocol AddTAFDataSource;
#interface AddTAFViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
id<AddTAFDataSource> dataSource;
NSString *newICAOCode;
}
#property (nonatomic, assign) id<AddTAFDataSource> dataSource;
- (IBAction)saveButtonPressed;
#end
#protocol AddTAFDataSource <NSObject>
- (void)addNewTAF:(AddTAFViewController *)addTAFViewController icao:(NSString *)icaoCode;
#end
The implementation file (modal view controller):
#import "AddTAFViewController.h"
#import "TAFandMETARViewController.h"
#implementation AddTAFViewController
#synthesize dataSource;
...
- (IBAction)saveButtonPressed {
[self.dataSource addNewTAF: self icao: newICAOCode];
}
#end
Presenting view controller header file:
#import "AddTAFViewController.h"
#interface TAFandMETARViewController : UITableViewController <AddTAFDataSource> {
}
#end
And finally, the presenting view controller:
#import "AddTAFViewController.h"
...
- (void)insertNewObject:(id)sender {
AddTAFViewController *addTAFViewController = [[AddTAFViewController alloc] initWithNibName: #"AddTAF" bundle: [NSBundle mainBundle]];
addTAFViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[(AddTAFViewController *)self.view setDataSource: self];
[self presentModalViewController: addTAFViewController animated: YES];
addTAFViewController = nil;
[addTAFViewController release];
}
- (void)addNewTAF:(AddTAFViewController *)addTAFViewController icao:(NSString *)icaoCode {
newICAO = icaoCode;
[self dismissModalViewControllerAnimated: YES];
}
Just to remind, it is the above -(void)addNewTAF: method that does not get messaged. Any help/pointers in the right direction are much appreciated.
Replace:
[(AddTAFViewController *)self.view setDataSource: self];
With:
[addTAFViewController setDataSource:self]
After all, the dataSource is a property of the controller, not a controller's view.
Rather than trying to use a separate object (your dataSource) to pass data between the two view controllers, you could simply use add properties to contain the data directly in the view controller you're going to present modally (here, the AddTAFViewController).
Then in the method you use to dismiss the modal view controller, before dismissing it you can send [self modalViewController] to get the modal view controller, and at that point the parent view controller can send it any messages it wants. That would allow you to grab whatever data you need from the modal view controller, so you wouldn't need the data source and the protocol at all.
You are wrong at this point:
[(AddTAFViewController *)self.view setDataSource: self];
you should write this instead:
addTAFViewController.dataSource = self;