Issues in UINavigationController in Xcode 4.3.2? - iphone

I am working in iPhone application, Using XCode 4.3.2 tool develop my application (not using story board). When I press a button from homescreen.m to navigate to Login screen, then I run the app, cannot navigate from home to login screen, how to fix this issue?
I tried this:
Class Name - Appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
home = [[HomeScreen alloc]init];
UINavigationController *navi =[[UINavigationController alloc]initWithRootViewController:home];
[self.window addSubview:navi.view];
return YES;
}
Class Name - HomeScreen.m
#import "HomeScreen.h"
#import "LoginScreen.h"
#interface HomeScreen ()
#end
#implementation HomeScreen
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title=#"HomeScreen";
UIButton *Button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
Button1.frame = CGRectMake(10, 100, 100, 50);
[Button1 setTitle:#"Homescreen" forState:UIControlStateNormal];
[Button1 addTarget:self action:#selector(GotONext) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:Button1];
// Do any additional setup after loading the view.
}
-(void)GotONext
{
LoginScreen *log =[[LoginScreen alloc]init];
[self.navigationController pushViewController:log animated:YES];
}
Class Name - LoginScreen.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.title=#"LoginScreen";
}

try bellow code...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
home = [[[HomeScreen alloc] initWithNibName:#"HomeScreen" bundle:nil] autorelease];
UINavigationController *navviewController1=[[UINavigationController alloc]initWithRootViewController: HomeScreen];
self.window.rootViewController = navviewController1;
return YES;
}
and on next button method use bellow code...
-(void)GotONext
{
LoginScreen *log =[[LoginScreen alloc]initWithNibName:#"LoginScreen" bundle:nil] autorelease];
[self.navigationController pushViewController:log animated:YES];
}
i hope this help you...
:)

I think you are forgot this thing to write initwithNibName:#"LogonScreen".
-(void)GotONext
{
LoginScreen *log =[[LoginScreen alloc] initWithNibName:#"LoginScreen" bundle:nil];
[self.navigationController pushViewController:log animated:YES];
}

Related

UIButton created programmatically but doesn't show up on view (addSubview was called)

Have an AppDelegate and a MainViewController.
In MainViewController the UIButton was created, AppDelegate sets a background image and should show it on MainViewController. That's pretty much it but I can't see the UIButton.
Just for alignment purpose I created an IBOutlet and connected the button in IB.
MainViewController.h
#property (strong, nonatomic) IBOutlet UIButton *searchBtn;
MainViewController.m
#synthesize searchBtn;
AppDelegate.m
#synthesize mainVC;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ViewController > setup:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.mainVC = [[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
self.window.rootViewController = self.mainVC;
self.window.backgroundColor = [UIColor whiteColor];
// Buttons > Set background images:
[self.mainVC.searchBtn setImage:[UIImage imageNamed:#"search.png"] forState:UIControlStateNormal];
[self.mainVC.view addSubview:self.mainVC.searchBtn];
[self.window makeKeyAndVisible];
return YES;
}
First you don't need IBOutlet if you are creating it programmatically.
Second you probably want to move the creation of the button in viewDidLoad of your view controller rather than in your AppDelegate. It's your view controller's business.
Third you should probably alloc init your button if you create it programmatically:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(...)];
[button setBackgroundImage:someImage];
[button addTarget:self action:#selector(something:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
something like that.
You should be adding buttons in your MainViewController class, not the AppDelegate first off.
You should also set the background of the view in MainViewController, not the AppDelegate.
It should look like this:
AppDelegate.m
#synthesize mainVC;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ViewController > setup:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.mainVC = [[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
self.window.rootViewController = self.mainVC;
[self.window makeKeyAndVisible];
return YES;
}
In your MainViewController.m's viewDidLoad put this
- (void)viewDidLoad {
[super viewDidLoad];
// Buttons > Set background images:
[searchBtn setBackgroundImage:[UIImage imageNamed:#"search.png"] forState:UIControlStateNormal];
//If you adding the button in Interface Builder you don't need to add it again
//[self.view addSubview:self.mainVC.searchBtn];
}

presenting modal on top of MPMoviePlayerController leads to crash, but only in iOS < 5.0

So, here's the situation (this is all in the simulator):
create a view controller. it owns an MPMoviePlayerController, and adds the player's view as a subview to its own.
set this vc as the window's rootViewController.
also in this vc's view is a button which will launch a modal view controller (which obscures the movie) after pausing the movie player.
click the button, launch the modal VC, then dismiss it.
notice that the paused image of the movie player is gone (it is just black).
click "play" to resume playing the movie
crash
Now, change your build target from iOS 4.3 to iOS 5.0 and it works flawlessly.
What's really bothering me is that this didn't cause a crash under 4.3 a month ago. In fact, I have an app in the store right now for which this works just fine: http://itunes.apple.com/us/app/es-musica-free/id474811522?mt=8
So, I've tried to boil this down to a minimal example which reproduces the problem.
Create a vanilla xcode project.
in your AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.window.backgroundColor = [UIColor blackColor];
[self.window makeKeyAndVisible];
UIViewController *vc = [[DemoMoviePlayerViewController alloc] init];
[self.window setRootViewController:vc];
[vc release];
vc = nil;
return YES;
}
DemoMoviePlayerViewController is pretty simple. here's the h file:
#import
#import
#interface DemoMoviePlayerViewController : UIViewController
{
MPMoviePlayerController *moviePlayer_;
}
#end
and the m file:
#import "DemoMoviePlayerViewController.h"
#interface DemoMoviePlayerViewController (InternalMethods)
- (void)_buttonPressed:(UIButton*)button;
#end
#implementation DemoMoviePlayerViewController
- (id)init
{
self = [super init];
if (self == nil)
{
return nil;
}
[self setWantsFullScreenLayout:YES];
NSURL *url = [NSURL URLWithString:#"http://once.unicornmedia.com/now/od/auto/0116d5af-bdc1-456d-a3ac-e3cbe25bac98/c762a821-e583-420c-b798-7b8c66736027/6c0e77e0-6dc4-48d5-9197-26e59271e8dd/content.once"];
moviePlayer_ = [[MPMoviePlayerController alloc] initWithContentURL:url];
return self;
}
- (void)dealloc
{
[moviePlayer_ stop];
[moviePlayer_ release];
moviePlayer_ = nil;
[super dealloc];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
[[self view] setBackgroundColor:[UIColor redColor]]; // debug
[[moviePlayer_ view] setFrame:[[self view] bounds]];
[[moviePlayer_ view] setAutoresizingMask:(UIViewAutoresizingFlexibleWidth
|UIViewAutoresizingFlexibleHeight)];
[moviePlayer_ setControlStyle:MPMovieControlStyleFullscreen];
[[moviePlayer_ view] setBackgroundColor:[UIColor greenColor]]; // debug
[[self view] addSubview:[moviePlayer_ view]];
// add a button to launch share kit
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"launch modal" forState:UIControlStateNormal];
[button addTarget:self
action:#selector(_buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[[moviePlayer_ view] addSubview:button];
[button setFrame:CGRectMake(20, 100, 200, 40)];
[moviePlayer_ setShouldAutoplay:YES];
[moviePlayer_ play];
}
#pragma mark - internal methods -
- (void)_dismissModal
{
[self dismissModalViewControllerAnimated:YES];
}
- (void)_buttonPressed:(UIButton*)button
{
if ([moviePlayer_ currentPlaybackRate] == 1.0)
{
[moviePlayer_ setShouldAutoplay:NO];
[moviePlayer_ pause];
}
[self performSelector:#selector(_dismissModal) withObject:nil afterDelay:3.0];
UIViewController *vc = [[UIViewController alloc] init];
[[vc view] setBackgroundColor:[UIColor blueColor]];
[self presentModalViewController:vc animated:YES];
// yeah I know, vc is leaked...
}
#end
So apparently, somehow the MPMoviePlayerController is somehow able to detect which its view gets obscured, despite the fact that my view controller isn't doing anything fancy in viewWillDisappear or viewDidDisappear.
Again, what really bothers me is that, to the best of my knowledge, this worked just fine under 4.3 about a month ago.

Can't dismiss modal view when tap a button

In "FirstViewController" I declare a button which present the modal view "InfoViewController".
In "InfoViewController", I declare a toolbar with a "modalViewButton" UIButton which dismiss the modal view. But the "OK" UIButton doesn't work. I don't know why.
Here's FirstViewController.h
#import <UIKit/UIKit.h>
#import "InfoViewController.h"
#interface FirstViewController : UIViewController
{
InfoViewController *infoViewController;
}
#property (nonatomic, retain) InfoViewController *infoViewController;
#end
Here's FirstViewController.m
#import "FirstViewController.h"
#implementation FirstViewController
#synthesize infoViewController;
- (IBAction)modalViewAction:(id)sender
{
if (self.infoViewController == nil)
self.infoViewController = [[[InfoViewController alloc] initWithNibName:
NSStringFromClass([InfoViewController class]) bundle:nil] autorelease];
[self presentModalViewController:self.infoViewController animated:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[infoViewController release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton* modalViewButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[modalViewButton addTarget:self
action:#selector(modalViewAction:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *modalBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:modalViewButton];
self.navigationItem.leftBarButtonItem = modalBarButtonItem;
[modalBarButtonItem release];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
Here's InfoViewController.h
#import <UIKit/UIKit.h>
#interface InfoViewController : UIViewController
{
}
-(IBAction)infoDismissAction:(id)sender;
#end
Here's the InfoViewController.m
#import "InfoViewController.h"
#implementation InfoViewController
- (IBAction)infoDismissAction:(id)sender
{
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *infoLabel = [[UILabel alloc] init];
infoLabel.frame = CGRectMake(50, 100, 100, 40);
infoLabel.textAlignment = UITextAlignmentCenter;
infoLabel.text = #"About";
[self.view addSubview:infoLabel];
UIToolbar *toolBar;
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
toolBar.frame = CGRectMake(0, 0, 320, 50);
toolBar.barStyle = UIBarStyleDefault;
[toolBar sizeToFit];
UIBarButtonItem *flexibleSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil] autorelease];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"OK"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(infoDismissAction:)];
UIBarButtonItem* infoTitle = [[UIBarButtonItem alloc] initWithTitle:#"About"
style:UIBarButtonItemStylePlain
target:self action:nil];
NSArray *barButtons = [[NSArray alloc] initWithObjects:flexibleSpace,flexibleSpace,infoTitle,flexibleSpace,doneButton,nil];
[toolBar setItems:barButtons];
[self.view addSubview:toolBar];
[toolBar release];
[infoTitle release];
[doneButton release];
[barButtons release];
[infoLabel release];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
I would solve this issue with a delegate method.
First make a protocol in your modalViewController
#protocol ModalViewDelegate <NSObject>
- (void)didDismissModalView;
#end
And set a delegate property in the same modalVC:
id<ModalViewDelegate> dismissDelegate;
Then make a buttonActionMethod that calls the delegate in the modalVC:
- (void)methodCalledByButton:(id)sender
{
// Call the delegate to dismiss the modal view
[self.dismissDelegate didDismissModalView];
}
Now your modalVC is done you have to prepare the mainVC calling the modalVC:
You have to make your MainViewController comform to the delegate:
#interface MainViewController : UIViewController <ModalViewDelegate>
At the place you alloc your ModalViewController you have to set the delegate property you made in your modalViewController:
self.myModalViewController.dismissDelegate = self;
Now the MainViewController listens to the delegate and the only thing you need to do is implement the delegateMethod.
-(void)didDismissModalView
{
[self dismissModalViewControllerAnimated:YES];
}
Now your ModalVC will dismiss on a buttonpress (at least when you call the method properly)
Hope this all makes sense.
Good luck.
You can only dismiss currently displayed modal view, so in your method infoDismissAction: you should do one of following
1) [self dismissModalViewControllerAnimated:YES];
2) Send to parent view controller message that current modal view should be dismissed and send reference to that view.
Second approach is better as it is more safe.
In your -infoDismissAction try to call [self dismissModalViewControllerAnimated:YES];
Here the best sample code for the model view for iphone and ipad also.
The popups have a number of configurable items. They can be animated to either slide or popup onto the display. Once visible, they can be dismissed either by tapping the screen or after a programmed delay. The background and text colors can also be adjusted however you like.
Download the sample code from here.
The current answers are deprecated. Here is the updated code:
[self dismissViewControllerAnimated:NO completion:nil];

Why Isn't My Info Button Working?

My Info button is showing up when I run my app but it doesn't do anything, no response when I click it.
In my ProfileViewController file:
- (void)viewDidLoad
{
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoDark];
infoButton.frame = CGRectMake(290.0, 10.0, 15.0, 15.0);
[infoButton addTarget:self action:#selector(toggleCreditsOpen:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:infoButton];
[super viewDidLoad];
}
I also have the following two methods to load the about view (the screen that loads up when the button is clicked):
- (IBAction) toggleCreditsOpen:(id)inSender
{
UIViewController *theController = [[UIViewController alloc] initWithNibName:#"AboutViewController" bundle:nil];
[self.navigationController presentModalViewController:theController animated:TRUE];
}
- (IBAction) toggleCreditsClosed:(id)inSender
{
[self.navigationController dismissModalViewControllerAnimated:TRUE];
}
EDIT:
I am adding my full implementation file here:
#import "ProfileViewController.h"
#import "AboutViewController.h"
#implementation ProfileViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (IBAction)toggleCreditsOpen:(id)inSender
{
UIViewController *theController = [[UIViewController alloc] initWithNibName:#"AboutViewController" bundle:nil];
[self.navigationController presentModalViewController:theController animated:YES];
}
- (IBAction)toggleCreditsClosed:(id)inSender
{
[self.navigationController dismissModalViewControllerAnimated:TRUE];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
UIButton *infoButton = [[UIButton buttonWithType:UIButtonTypeInfoDark] retain];
infoButton.frame = CGRectMake(290.0, 10.0, 15.0, 15.0);
//[infoButton addTarget:self action:#selector(toggleCreditsOpen:)forControlEvents:UIControlEventTouchUpInside];
[infoButton addTarget:self action:#selector(toggleCreditsOpen:)forControlEvents:UIControlEventTouchDown];
[self.view addSubview:infoButton];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
Using your provided code works for me, if self.navigationController is non-nil, and there exists an AboutViewController.xib in your main bundle. Otherwise, I don't see anything readily wrong.
If you don't have a navigation controller, the proper line in toggleCreditsOpen: would be:
[self presentModalViewController:theController animated:YES];
Edit:
If you want to dismiss the modal view later, its usually a good idea to do this with a delegate. Instead of
UIViewController *theController = [[UIViewController alloc] initWithWhatever];
you can do
AboutViewController *theController = [[AboutViewController alloc] initWithWhatever];
where AboutViewController has a delegate. Probably something like id<DismissModalProtocol> delegate;. Then, your view controller would implement this #protocol, and before it calls presentModalViewController:animated:, it'll set itself as the delegate. So very roughly, it would look something like this:
// AboutViewController.h
#protocol DismissModalProtocol;
#interface AboutViewController : UIViewController {
id<DismissModalProtocol> delegate;
}
#property id<DismissModalProtocol> delegate;
#end
#protocol DismissModalProtocol <NSObject>
- (void)dismissController:(UIViewController *)viewController;
#end
// ProfileViewController.h
#import "AboutViewController.h"
#interface ProfileViewController : UIViewController <DismissModalProtocol>
#end
// ProfileViewController.m
#implementation ProfileViewController
- (void)dismissController:(UIViewController *)viewController {
[self dismissModalViewControllerAnimated:YES];
}
- (void)toggleCreditsOpen:(id)sender {
AboutViewController *controller = [[AboutViewController alloc] init];
controller.delegate = self;
[self presentModalViewController:controller animated:YES];
}
#end
I don't know at all why this isn't working, but if I were to take a guess, I'd say it's in the [self.navigationController presentModalViewController:theController animated:TRUE];
statement. From what I know, it should be:
[self.navigationController presentModalViewController:theController animated:YES];
See if it works after you do that. I'm not sure, but that could be the problem with your code.
hi
you are took a mistake to define your button..
just assign retain property at the end of button
like
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoDark] retain];
and also give
[infoButton addTarget:self action:#selector(toggleCreditsOpen:)forControlEvents:UIControlEventTouchDown];

UINaviagtionBar not displaying?

I developing an application in which initially choose UINavigationController based application.
In rooViewController xib i added two more UIView, the code is written as follows.
#implementation RootViewController
#synthesize introductionView, WheelView;
- (void)viewDidLoad {
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES];
}
#pragma mark
#pragma mark strart up screen:
-(void)startIntroductionScreen{
if(window == nil) {
window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
}
[window addSubview: introductionView];
[window makeKeyAndVisible];
}
-(void)WheelScreen{
[window addSubview:carnivalWheelView];
[window makeKeyAndVisible];
self.navigationController.navigationBar.hidden = NO;
[self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:#"Setting" style:UIBarButtonItemStylePlain target:nil action:nil] animated:YES];
}
#pragma mark
#pragma mark IBAction:
-(IBAction)breakGlass:(id)sender{
[self startIntroductionScreen];
}
-(IBAction)anotherView:(id)sender{
[self WheelScreen];
}
In above code, when the -(void)WheelScreen method on the IBAction anotherView of invoke the UINaviagtionBar is not displaying.
Thank's in advance.
Hi you are adding your view directly to the Window, and it probably has the height 480 px?
This means you are covering up the navigationController with the carnivalWheelView.
The carnivalWheel should be a subclass of UIViewController, you should then add it to the navigationController instead of the window.
CarnivalWheel *cw = [[CarnivalWheel alloc] init];
[self.navigationController pushViewController:cw animated:YES];
[cw release];
Now the CarnivalWheel is a "child" of the navigationController and will not cover it with its view.