Can't dismiss modal view when tap a button - iphone

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

Related

UIImagePickerController get image in an inherited

I searched the internet for a solution but unfortunately I'm not able to find a solution to my problem.
Most people are creating an object of the UIImagePickerController class but I decided to do this on different way.
I created a new class CameraViewController this class extends of the UIImagePickerController.
My .h file:
#import <UIKit/UIKit.h>
#interface CameraViewController : UIImagePickerController <UIImagePickerControllerDelegate>
-(void)captureImage;
#end
My .m file:
#import "CameraViewController.h"
#define CAMERA_TRANSFORM_X 1
#define CAMERA_TRANSFORM_Y 1.5
#interface CameraViewController ()
#end
#implementation CameraViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
UIButton *btnCapture = [[UIButton alloc] initWithFrame:CGRectMake((320/2)-(76/2), 480, 76, 76)];
[btnCapture setImage:[UIImage imageNamed:#"cam.png"] forState:UIControlStateNormal];
[btnCapture addTarget:self action:#selector(captureImage) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:btnCapture];
self.sourceType = UIImagePickerControllerSourceTypeCamera;
self.showsCameraControls = NO;
self.navigationBarHidden = YES;
self.wantsFullScreenLayout = YES;
self.cameraViewTransform = CGAffineTransformScale(self.cameraViewTransform, CAMERA_TRANSFORM_X, CAMERA_TRANSFORM_Y);
self.cameraOverlayView = view;
//[self openCamera];
// Do any additional setup after loading the view.
}
-(void)takePicture //overwrite take picture method
{
[super takePicture];
NSLog(#"%#", self);
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSLog(#"%#", info);
}
-(void)captureImage
{
[self takePicture];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I implemented the delegate UIImagePickerControllerDelegate. But it seems that my imagePickerController is not called for some reason. In this method I try to log a NSDirectory. So I thought maybe I can get my image by overwriting the takePicture method, but I have no idea how. Anyone know how to help me? Thanks a lot
From apple docs The UIImagePickerController class supports portrait mode only. This class is intended to be used as-is and does not support subclassing.
So you can't subclass UIImagePickerController.

NSString from other UIViewController is null

Could anyone please explain to me why this isn't working? I think it is a real simple problem, but for some reason I am not getting it.
My problem: I have a NSString in my firstViewController with value "Hello!". Now I want to display this value in my secondViewController but the output is null.
This is my NSLog
2013-03-09 foo[95381:c07] value of foo in BITfirstViewcontroller: Hello!
2013-03-09 foo[95381:c07] value of foo in BITsecondViewcontroller: (null)
BITAppDelegate.m
#import "BITAppDelegate.h"
#import "BITfirstViewController.h"
#implementation BITAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
BITfirstViewController *fvc = [[BITfirstViewController alloc]init];
// Create navigationController and set InputViewController as root for navController
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:fvc];
[self.window setRootViewController:navController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
BITfirstViewController.h
#import <UIKit/UIKit.h>
#interface BITfirstViewController : UIViewController
#property (strong, nonatomic) NSString *foo;
-(IBAction)showSecondView:(id)sender;
#end
BITfirstViewController.m
#import "BITfirstViewController.h"
#import "BITsecondViewController.h"
#interface BITfirstViewController ()
#end
#implementation BITfirstViewController
#synthesize foo;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
foo = [NSString stringWithFormat:#"Hello!"];
NSLog(#"value of foo in BITfirstViewcontroller: %#",foo);
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(showSecondView:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)showSecondView:(id)sender;
{
BITsecondViewController *svc = [[BITsecondViewController alloc]init];
[self.navigationController pushViewController:svc animated:YES];
}
#end
BITsecondViewController.h
#import <UIKit/UIKit.h>
#class BITfirstViewController;
#interface BITsecondViewController : UIViewController
#property (nonatomic, retain) BITfirstViewController *fvc;
#end
BITsecondViewController.m
#import "BITsecondViewController.h"
#import "BITfirstViewController.h"
#interface BITsecondViewController ()
#end
#implementation BITsecondViewController
#synthesize fvc;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(#"value of foo in BITsecondViewcontroller: %#", fvc.foo);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Try this in BITfirstViewController.m
-(IBAction)showSecondView:(id)sender;
{
BITsecondViewController *svc = [[BITsecondViewController alloc]init];
svc.fvc = self; // you need to set this property as you prepare the svc
[self.navigationController pushViewController:svc animated:YES];
}
EDIT:
Your fvc and svc are encapsulated, and don't have direct knowledge about each-other except though the use of properties (at least that's the way we try to do things in OOP). So, you were correct in creating a #property in your svc that allowed you to have a handle to your fvc, but [[BITsecondViewController alloc]init] will simply initialize that pointer to nil. Then you need to set that property to point to the intended object, which in this case is the fvc. Since the svc is being build from inside an instance of BITFirstViewController, the correct handle to pass in is self.

iPhone Objective C - can't display new view

I'm finding it difficult to get started in Objective-C.
I'm executing the following code when a button is clicked:
NSLog(#"hi");
MainMenuDriver *mainMenuDriver= [[MainMenuDriver alloc] initWithNibName:nil bundle:nil];
[[self navigationController]pushViewController:mainMenuDriver animated:YES];
I can see "hi" in the console when I hit the button, it's just that the view should change to MainMenuDriver. But nothing happens!
Please help!
As per request for more code:
MainMenuDriver.h:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface MainMenuDriver : UIViewController <UINavigationControllerDelegate,CLLocationManagerDelegate>{
IBOutlet UIButton *photos;
IBOutlet UIButton *profile;
IBOutlet UISwitch *onOffline;
IBOutlet UILabel *label1;
NSTimer *uploadGPS_timer;
CLLocationManager *lm;
NSString *lat;
NSString *lng;
}
#property(nonatomic,retain) UIButton *photos;
#property(nonatomic,retain) UIButton *profile;
#property(nonatomic,retain) UISwitch *onOffline;
#property(nonatomic,retain) UILabel *label1;
#property (nonatomic,retain) NSTimer *uploadGPS_timer;
#property(nonatomic,retain) NSString *lat,*lng;
-(IBAction)showMessages:(id)sender;
-(IBAction)showFriends:(id)sender;
-(IBAction)showPhotos:(id)sender;
-(IBAction)showProfile:(id)sender;
-(IBAction)switchSwitched:(id)sender;
-(void)uploadGPS_tick:(NSTimer*)timer;
#end
MainMenuDriver.m
#import "MainMenuDriver.h"
#import "ASIFormDataRequest.h"
#import "JoeMaxiViewController.h"
#import "Photos.h"
#import "Profile.h"
#implementation MainMenuDriver
#synthesize messages,profile,photos,friends,label1;
#synthesize onOffline;
#synthesize uploadGPS_timer;
#synthesize lat,lng;
-(IBAction)showPhotos:(id)sender{
[self.navigationItem setBackBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil]];
Photos *x= [[Photos alloc] initWithNibName:nil bundle:nil];
[[self navigationController]pushViewController:x animated:YES];
}
-(IBAction)showProfile:(id)sender{
[self.navigationItem setBackBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil]];
Profile *x= [[Profile alloc] initWithNibName:nil bundle:nil];
[[self navigationController]pushViewController:x animated:YES];
}
-(void)logout:(id)sender{
if([uploadGPS_timer isValid]){
[uploadGPS_timer invalidate];
}
[lm release];
//[uploadGPS_timer release];
[self.navigationController popViewControllerAnimated:NO];
/*NSString *urlStr=[[NSString alloc] initWithFormat:#"http://www.prestocab.com/driver/ajax/logout.php"];
NSURL *url=[NSURL URLWithString:urlStr];
__block ASIFormDataRequest *request=[[ASIFormDataRequest alloc ]initWithURL:url];
[request setDelegate:self];
[request setCompletionBlock:^{
NSString *response=[request responseString];
NSLog(#"%#",response);
}];
[request setFailedBlock:^{
}];
[request startAsynchronous];*/
}
-(IBAction)switchSwitched:(id)sender{
if(onOffline.on){
[label1 setText:#"For Hire"];
[label1 setTextColor:[UIColor colorWithRed:0.0 green:0.8 blue:0.0 alpha:1.0]];
uploadGPS_timer=[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:#selector(uploadGPS_tick:) userInfo:nil repeats:YES];
[self uploadGPS_tick:nil];
}else{
[label1 setText:#"Engaged"];
[label1 setTextColor:[UIColor colorWithRed:0.8 green:0.0 blue:0.0 alpha:1.0]];
if([uploadGPS_timer isValid]){
[uploadGPS_timer invalidate];
}
}
}
-(void)uploadGPS_tick:(NSTimer*)timer{
if(!lat || !lng){
//do nothing
}else{
NSString *urlStr=[[NSString alloc] initWithFormat:#"http://www.prestocab.com/driver/ajax/updateCoords.php"];
NSURL *url=[NSURL URLWithString:urlStr];
__block ASIFormDataRequest *request=[[ASIFormDataRequest alloc ]initWithURL:url];
[request setPostValue:lat forKey:#"lat"];
[request setPostValue:lng forKey:#"lng"];
NSLog(#"EOH: %#",lat);
[request setDelegate:self];
[request setCompletionBlock:^{
NSString *response=[request responseString];
NSLog(#"%#",response);
//do nothing
}];
[request setFailedBlock:^{
//NSError *error =[request error];
//do nothing
}];
[request startAsynchronous];
}
}
-(void)locationManager:(CLLocationManager*) manager didUpdateToLocation:(CLLocation *) newLocation fromLocation:(CLLocation*) oldLocation{
lat=[[NSString alloc]initWithFormat:#"%g",newLocation.coordinate.latitude];
lng=[[NSString alloc]initWithFormat:#"%g",newLocation.coordinate.longitude];
NSLog(#"%#,%#",lat,lng);
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (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.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
self.navigationItem.title=#"PrestoCab";
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Logout" style:UIBarButtonItemStylePlain target:self action:#selector(logout:)];
self.navigationItem.rightBarButtonItem = anotherButton;
[anotherButton release];
//GPS
lm=[[CLLocationManager alloc] init];
if([CLLocationManager locationServicesEnabled]){
lm.delegate=self;
lm.desiredAccuracy=kCLLocationAccuracyBest;
lm.distanceFilter=30.0f;
[lm startUpdatingLocation];
}
//[self check4messages_tick:nil]; //want to start immediately, not in 10/40 seconds' time
[self uploadGPS_tick:nil];
//check4messages_timer=[NSTimer scheduledTimerWithTimeInterval:40.0 target:self selector:#selector(check4messages_tick:) userInfo:nil repeats:YES];
uploadGPS_timer=[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:#selector(uploadGPS_tick:) userInfo:nil repeats:YES];
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
if([uploadGPS_timer isValid]){
[uploadGPS_timer invalidate];
}
}
-(void)dealloc{
[super dealloc];
[uploadGPS_timer release];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
Also, here is
MyClassViewController.h:
#import <UIKit/UIKit.h>
#interface MyClassViewController : UINavigationController{
IBOutlet UIButton *passenger;
IBOutlet UIButton *driver;
}
#property (nonatomic,retain) UIButton *passenger;
#property (nonatomic,retain) UIButton *driver;
-(IBAction) passengerClicked:(id)sender;
-(IBAction) driverClicked:(id)sender;
#end
and MyClassViewController.m:
#import "MyClassViewController.h"
#import "MainMenuDriver.h"
#implementation MyClassViewController
#synthesize passenger;
#synthesize driver;
-(IBAction)passengerClicked:(id)sender{
NSLog(#"passenger");
}
-(IBAction)driverClicked:(id)sender{
NSLog(#"driver");
MainMenuDriver *mainMenuDriver= [[MainMenuDriver alloc] initWithNibName:nil bundle:nil];
[[self navigationController]pushViewController:mainMenuDriver animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[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.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
I can see a couple of things:
Your nib file's name is different from the name of your class. If you put nil, the UIViewController will try to load a nib file with the UIViewController's name. Your UIViewController can be MainMenuDriver but your nib file name can be MainMenuDriverNibFileName.nib
[self navigationController] is nil.
For this, do the following:
NSLog(#"hi");
if([self navigationController]){
MainMenuDriver *mainMenuDriver= [[MainMenuDriver alloc] initWithNibName:nil bundle:nil];
[[self navigationController]pushViewController:mainMenuDriver animated:YES];
}
else{
NSLog(#"Houston we have a problem");
}
Update 1:
So, its nil, what you can do is the following:
Quick and Dirty:
[self presentViewController:mainMenuDriver animated:YES];
Instead of UIViewController switch to a UINavigationController.

Navigation Controller in a Tab Controller not showing back button

I have a tab controller with 2 tabs "My information" and "their Information", which is just a bunch of text boxes. I want a Navigation controller at the top so I can go back, but the back button won't show up... here is my code:
Button pressed to show Form:
-(IBAction)onIncidentAidFormPressed:(id)sender {
FormController *aidForm = [[FormController alloc] initWithNibName:#"formController" bundle:nil];
aidForm.managedObjectContext = self.managedObjectContext;
[self.navigationController pushViewController:aidForm animated:YES];
}
In FormController.m - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil:
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Aid Form", #"Aid Form title");
}
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[MyInformationController alloc] initWithNibName:#"MyInformationController" bundle:nil];
UIViewController *viewController2 = [[TheirInformationController alloc] initWithNibName:#"TheirInformationController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithNibName:#"navController" bundle:nil];
[navController pushViewController:viewController1 animated:YES];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController, viewController2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return self;
The tabs show up correctly, and the navigation bar is there but no back button is shown to get back.
Edit: Here are the files: IncidentAidStartViewController.h
#import <UIKit/UIKit.h>
#interface IncidentAidStartViewController : UIViewController {
IBOutlet UIButton *incidentAidForm;
IBOutlet UIButton *emergencyContacts;
}
-(IBAction)onIncidentAidFormPressed:(id)sender;
#property (strong, nonatomic) IncidentAidStartViewController *detailViewController;
#property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#end
IncidentAidStartViewController.m
#import "IncidentAidStartViewController.h"
#import "IncidentAidForm.h"
#implementation IncidentAidStartViewController
#synthesize detailViewController = _detailViewController;
#synthesize managedObjectContext = __managedObjectContext;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Incident Aid", #"Incident Aid title");
}
return self;
}
#pragma mark - Button handlers
-(IBAction)onIncidentAidFormPressed:(id)sender {
IncidentAidForm *aidForm = [[IncidentAidForm alloc] initWithNibName:#"IncidentAidForm" bundle:nil];
aidForm.managedObjectContext = self.managedObjectContext;
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:nil];
[self.navigationController pushViewController:aidForm animated:YES];
}
- (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.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[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
IncidentAidForm.h
#import <UIKit/UIKit.h>
#interface IncidentAidForm : UIViewController <UITabBarControllerDelegate> {
}
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UITabBarController *tabBarController;
#property (strong, nonatomic) IncidentAidForm *detailViewController;
#property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#end
IncidentAidForm.m
#import "IncidentAidForm.h"
#import "MyInformationController.h"
#import "TheirInformationController.h"
#implementation IncidentAidForm
#synthesize detailViewController = _detailViewController;
#synthesize managedObjectContext = __managedObjectContext;
#synthesize window = _window;
#synthesize tabBarController = _tabBarController;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Incident Aid Form", #"Incident Aid Form title");
}
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[MyInformationController alloc] initWithNibName:#"MyInformationController" bundle:nil];
UIViewController *viewController2 = [[TheirInformationController alloc] initWithNibName:#"TheirInformationController" bundle:nil];
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController1, navController2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return self;
}
- (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.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[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
MyInformationController.h
#import <UIKit/UIKit.h>
#interface MyInformationController : UIViewController {
IBOutlet UIScrollView *scrollView;
IBOutlet UITextField *firstName;
IBOutlet UITextField *lastName;
IBOutlet UITextField *phoneNumber;
IBOutlet UITextField *address;
IBOutlet UITextField *carMake;
IBOutlet UITextField *carModel;
IBOutlet UITextField *carYear;
IBOutlet UITextField *licensePlate;
IBOutlet UITextField *insuranceCarrier;
IBOutlet UITextField *insurancePolicy;
IBOutlet UIButton *backgroundButton;
}
#property(nonatomic, retain) UIScrollView *scrollView;
#property (nonatomic, retain) IBOutlet UITextField *firstName;
#property (nonatomic, retain) IBOutlet UITextField *lastName;
#property (nonatomic, retain) IBOutlet UITextField *phoneNumber;
#property (nonatomic, retain) IBOutlet UITextField *address;
#property (nonatomic, retain) IBOutlet UITextField *carMake;
#property (nonatomic, retain) IBOutlet UITextField *carModel;
#property (nonatomic, retain) IBOutlet UITextField *carYear;
#property (nonatomic, retain) IBOutlet UITextField *licensePlate;
#property (nonatomic, retain) IBOutlet UITextField *insuranceCarrier;
#property (nonatomic, retain) IBOutlet UITextField *insurancePolicy;
- (void) animateTextField: (UITextField*) textField up: (BOOL) up;
-(IBAction)hideKeyboard;
#end
MyInformationController.m
#import "MyInformationController.h"
#implementation MyInformationController
#synthesize scrollView, firstName, lastName, phoneNumber, address, carMake, carModel, carYear, licensePlate,
insuranceCarrier, insurancePolicy;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"My Information", #"My Information");
self.tabBarItem.image = [UIImage imageNamed:#"first"];
}
return self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;
// Do any additional setup after loading the view, typically from a nib.
firstName.delegate = self;
lastName.delegate = self;
phoneNumber.delegate = self;
address.delegate = self;
carMake.delegate = self;
carModel.delegate = self;
carYear.delegate = self;
licensePlate.delegate = self;
insuranceCarrier.delegate = self;
insurancePolicy.delegate = self;
[scrollView setContentSize:self.view.frame.size];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
//[self animateTextField: textField up: YES];
scrollView.frame = CGRectMake(0,44,320,200); //44:NavigationBar ; 200: Keyoard
[scrollView scrollRectToVisible:textField.frame animated:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
//[self animateTextField: textField up: NO];
if (textField.tag == 10) {
scrollView.frame = CGRectMake(0,44,320,416); //original setup
// [textField resignFirstResponder];
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
switch (textField.tag) {
case 1:
[lastName becomeFirstResponder];
break;
case 2:
[phoneNumber becomeFirstResponder];
break;
case 3:
[address becomeFirstResponder];
break;
case 4:
[carMake becomeFirstResponder];
break;
case 5:
[carModel becomeFirstResponder];
break;
case 6:
[carYear becomeFirstResponder];
break;
case 7:
[licensePlate becomeFirstResponder];
break;
case 8:
[insuranceCarrier becomeFirstResponder];
break;
case 9:
[insurancePolicy becomeFirstResponder];
break;
case 10:
[textField resignFirstResponder];
break;
default:
break;
}
return TRUE;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (IBAction)hideKeyboard {
[self.firstName resignFirstResponder];
[self.lastName resignFirstResponder];
[self.phoneNumber resignFirstResponder];
[self.address resignFirstResponder];
[self.carMake resignFirstResponder];
[self.carModel resignFirstResponder];
[self.carYear resignFirstResponder];
[self.licensePlate resignFirstResponder];
[self.insurancePolicy resignFirstResponder];
[self.insuranceCarrier resignFirstResponder];
scrollView.frame = CGRectMake(0,44,320,416); //original setup
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
So far I know this is a good way to add ur views to tabbar. So try this
UIViewController *viewController1 = [[MyInformationController alloc] initWithNibName:#"MyInformationController" bundle:nil];
UIViewController *viewController2 = [[TheirInformationController alloc] initWithNibName:#"TheirInformationController" bundle:nil];
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController1, navController2, nil];
To add back bar item , below ine is also used.
self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;
Please modify your method like this
-(IBAction)onIncidentAidFormPressed:(id)sender
{
FormController *aidForm = [[FormController alloc] initWithNibName:#"formController" bundle:nil];
aidForm.managedObjectContext = self.managedObjectContext;
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:nil];
[self.navigationController pushViewController:aidForm animated:YES];
}
you should replace your code with the following:
UIViewController *viewController1 = [[MyInformationController alloc] initWithNibName:#"MyInformationController" bundle:nil];
UIViewController *viewController2 = [[TheirInformationController alloc] initWithNibName:#"TheirInformationController" bundle:nil];
UINavigationController *navController1 = [[[UINavigationController alloc] initWithRootViewController:viewController1] autorelease];
UINavigationController *navController2 = [[[UINavigationController alloc] initWithRootViewController:viewController2] autorelease];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController1, navController2, nil];
let me know any thing else you need........

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