Removing UIView from back/done button - iphone

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

Related

How to navigate from one view to another view in single viewcontroller in iphone sdk?

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.

Whose view is not in window hierarchy issue

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];

Dynamiclly creating View in iOS

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..!

UINavigationController pushViewController in viewDidLoad not working

I have the following code in my AppDelegate:
#import <UIKit/UIKit.h>
#class PersonalDiarySystemViewController;
#interface PersonalDiarySystemAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
PersonalDiarySystemViewController *viewController;
UINavigationController *navigationController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet PersonalDiarySystemViewController *viewController;
#property (nonatomic, retain) UINavigationController *navigationController;
#end
#import "PersonalDiarySystemAppDelegate.h"
#import "PersonalDiarySystemViewController.h"
#implementation PersonalDiarySystemAppDelegate
#synthesize window;
#synthesize viewController;
#synthesize navigationController;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Set the view controller as the window's root view controller and display.
self.window.rootViewController = self.viewController;
navigationController = [[UINavigationController alloc] initWithRootViewController:self.window.rootViewController];
navigationController.navigationBar.tintColor = [UIColor
colorWithRed:217.0/255
green:33.0/255
blue:0
alpha:1];
navigationController.navigationBarHidden = YES;
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
My rootviewcontroller tries to load another viewcontroller into the navigation controllers stack in its viewDidLoad method but for some reason the view is not getting pushed:
-(void) viewDidLoad{
lvc = [[LoginViewController alloc] init];
//lvc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[lvc setDelegate:self];
//[self presentModalViewController:lvc animated:YES];
[self.navigationController pushViewController:lvc animated:YES];
}
I'm getting no errors so not sure whats going on...using presentModalViewController works...so really am confused!!
You need to assign lvc to LoginViewController.
- (void) viewDidAppear
{
[self performSelector:#selector(loginCheck:) withObject:nil afterDelay:0.5];
}
- (void) loginCheck:(id)sender
{
LoginViewController * lvc = [[LoginViewController alloc] init];
//lvc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[lvc setDelegate:self];
//[self presentModalViewController:lvc animated:YES];
[self.navigationController pushViewController:lvc animated:YES];
}
Put your
[self.navigationController pushViewController:loginViewController];
Into the
- (void)viewDidAppear:(BOOL)animated
method. The viewControllers navigationController doesn't get loaded until then
There are two things that might go wrong.
First, you alloc the navigation controller in applicationDidFinishLaunching, I'm not quite sure which goes first, applicationDidFinishLaunching or viewDidLoad.
As you've seen, you first set your root view controller, then alloc the navigation controller, then maybe viewDidLoad launched right after you set the root view controller, then the navigation controller is allocated. so the words in viewDidLoad may not work because at that time, the navigation controller hasn't been born yet.
But I don't' quite thing the previous explanation works. it's just a possibility.
There's another strange thing, you set the navigation bar of your navigation controller hidden
navigationController.navigationBarHidden = YES;
Then it seems that the user can't pop back to the root view controller, so the navigation controller don't push the login view controller.
Meanwhile, the modal view controller can be dismissed with the navigation bar hidden or the navigation bar not allocated, so it works when you present it as a modal view controller.
but i'm still not quite sure about it since i'm now having some issues with Xcode, so i can't test the previous two ideas, sorry about that. but i still recommend that you set navigationBarHidden to NO.
- (void)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.navController = navigationController;
[mainViewController release];
[navigationController release];
// Configure and display the window.
[window addSubview:navController.view];
[window makeKeyAndVisible];
}
- (void)viewDidLoad{
lvc = [[LoginViewController alloc] init];
[self.navigationController pushViewController:lvc animated:YES];
}

self.view insertSubView .... not supposed to be self.view, but what?

I have a UIViewController which shows different UIViewController sub-classes. In the main UIViewController.m, I have a sub-class called 'Home' load on app start.
Now, which the Home view loaded, I have a button which I want to use to switch to another view called 'PreGameInfo'. I'm trying to use the code below:
- (IBAction)showPreGameInfo:(id)sender {
[self.view insertSubview:preGameInfo.view atIndex:0];
}
It doesn't work, and I know it's because the 'self.view' needs to refer to the main UIViewController rather than the self of the 'Home' view. Does anyone know how to insertSubView to the main UIViewController when using a UIButton whilst in a SubView???
Thank you!
You can use a delagate. It very easy
So implement this in your information view controller;
In the InformationViewController.h
#protocol InformationViewControllerDelegate;
#interface InformationViewController : UIViewController {
id <InformationViewControllerDelegate> delegate;
}
#property (nonatomic, assign) id <InformationViewControllerDelegate> delegate;
- (IBAction)returnBack:(id)sender;
#end
#protocol InformationViewControllerDelegate
- (void)InformationViewControllerDidFinish:(InformationViewController *)controller;
#end
in the InformationViewController.m
- (IBAction)returnBack:(id)sender {
[self.delegate InformationViewControllerDidFinish:self];
}
And use the delegate in any view controller you need it like this :
In the HomeViewController.h
#import "InformationViewController.h"
#interface HomeViewController : UIViewController <InformationViewControllerDelegate> {
}
Write the method to change the view from Home view to Information view
- (IBAction)goToInformationView:(id)sender;
In the HomeViewController.m
- (IBAction)goToInformationView:(id)sender {
InformationViewController *controller = [[InformationViewController alloc] initWithNibName:nil bundle:nil];
controller.delegate = self;
// You can chose the transition you want here (they are 4 see UIModalTransitionStyle)
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
And the last but not least the delegate method it inform the HomeViewController when the InformationViewController had finished
- (void)InformationViewControllerDidFinish:(InformationViewController *)controller {
[self dismissModalViewControllerAnimated:YES];
}
I hope it helps
- (IBAction)showPreGameInfo:(id)sender {
[superview insertSubview:preGameInfo.view atIndex:0];
}
Does this code work?
[self.parentViewController.view addSubview:myView];
Just do it in the modalView:
[self presentModalViewController:preGameInfo animated:YES]
or you can do something like this...
This line will add your new view to windows rootViewControllers view
[self.view.window.rootViewController.view addSubview:preGameInfo.view];