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........
Related
Every selectedSegmentIndex add different view controller.
On first segmentindex click, present new viewcontroller and second segment index, present another viewcontroller.
and segment control only show bottom.(not add in navigation bar).
segmentcontrol as it is visible every view controller(like tab bar controller).
my problem is that when new controller pop up segment controller disable.
Need help in getting this done.
Try this code
in .h file add
import
#interface SegmentManagingViewController : UIViewController <UINavigationControllerDelegate> {
UISegmentedControl * segmentedControl;
UIViewController * activeViewController;
NSArray * segmentedViewControllers;
IBOutlet UILabel *theLabel;
IBOutlet UIImageView *image;
}
//-(void)setTextColorsForSegmentedControl:(UISegmentedControl*)segmented;
#property (nonatomic, retain, readonly) IBOutlet UISegmentedControl * segmentedControl;
#property (nonatomic, retain, readonly) UIViewController * activeViewController;
#property (nonatomic, retain, readonly) NSArray * segmentedViewControllers;
#end
in .m file add
#import "SegmentManagingViewController.h"
#import "CategoryViewController.h"
#import "AtoZViewController.h"
#interface SegmentManagingViewController ()
#property (nonatomic, retain, readwrite) IBOutlet UISegmentedControl * segmentedControl;
#property (nonatomic, retain, readwrite) UIViewController * activeViewController;
#property (nonatomic, retain, readwrite) NSArray * segmentedViewControllers;
- (void)didChangeSegmentControl:(UISegmentedControl *)control;
- (NSArray *)segmentedViewControllerContent;
#end
#implementation SegmentManagingViewController
#synthesize segmentedControl, activeViewController, segmentedViewControllers;
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title =#"Breadworks";
//self.navigationItem.
[image release];
theLabel = [[UILabel alloc] initWithFrame:CGRectMake(140,-40,180,60)];
theLabel.backgroundColor = [UIColor colorWithRed:120.0f/255.0f green:69.0f/255.0f blue:53.0f/255.0f alpha:1.0f ];
[theLabel setFont:[UIFont fontWithName:#"Noteworthy " size:15]];
//[self.view addSubview:theLabel];
self.segmentedViewControllers = [self segmentedViewControllerContent];
//self.navigationController.navigationBarHidden=YES;
NSArray * segmentTitles = [self.segmentedViewControllers arrayByPerformingSelector:#selector(title)];
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTitles];
self.segmentedControl.tintColor = [UIColor colorWithRed:255.0f/255.0f green:252.0f/255.0f blue:235.0f/255.0f alpha:1.0f];
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:120.0f/255.0f green:69.0f/255.0f blue:53.0f/255.0f alpha:1.0f ];
self.segmentedControl.frame=CGRectMake(0,0,768, 73);
self.segmentedControl.selectedSegmentIndex = 0;
self.segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
//self.navigationController.navigationBar.frame = CGRectMake(0, 0,760, 100);
[self.segmentedControl addTarget:self
action:#selector(didChangeSegmentControl:)
forControlEvents:UIControlEventValueChanged];
self.view.frame = CGRectMake(0,4,330, 53);
self.view.backgroundColor = [UIColor colorWithRed:255.0f/255.0f green:252.0f/255.0f blue:235.0f/255.0f alpha:1.0f];
[segmentedControl setTitle:#"1" forSegmentAtIndex:0]
[segmentedControl setTitle:#"2" forSegmentAtIndex:1];
[self.view addSubview:segmentedControl];
[self.segmentedControl release];
[self didChangeSegmentControl:self.segmentedControl]; // kick everything off
}
- (NSArray *)segmentedViewControllerContent {
UIViewController * controller1 = [[CategoryViewController alloc] initWithParentViewController:self];
UIViewController * controller2 =[[AtoZViewController alloc] initWithParentViewController:self] ;
NSArray * controllers = [NSArray arrayWithObjects:controller1, controller2, nil];
[controller1 release];
[controller2 release];
return controllers;
}
#pragma mark -
#pragma mark Segment control
- (void)didChangeSegmentControl:(UISegmentedControl *)control {
if (self.activeViewController) {
[self.activeViewController viewWillDisappear:NO];
[self.activeViewController.view removeFromSuperview];
[self.activeViewController viewDidDisappear:NO];
}
//self.segmentedControl.frame=CGRectMake(5,4,300, 50);
self.activeViewController = [self.segmentedViewControllers objectAtIndex:control.selectedSegmentIndex];
[self.activeViewController viewWillAppear:NO];
[self.view addSubview:self.activeViewController.view];
[self.activeViewController viewDidAppear:NO];
NSString * segmentTitle = [control titleForSegmentAtIndex:control.selectedSegmentIndex];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:segmentTitle style:UIBarButtonItemStylePlain target:nil action:nil];
/*if(self.segmentedControl.selectedSegmentIndex == 0)
{
segmentedControl.tintColor = [UIColor colorWithRed:255.0f/255.0f green:252.0f/255.0f blue:235.0f/255.0f alpha:10.0f];
}
else
{
//self.segmentedControl.tintColor = [UIColor redColor];
}*/
}
#pragma mark -
#pragma mark View life cycle
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.activeViewController viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.activeViewController viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.activeViewController viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[self.activeViewController viewDidDisappear:animated];
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
for (UIViewController * viewController in self.segmentedViewControllers) {
[viewController didReceiveMemoryWarning];
}
}
- (void)viewDidUnload {
self.segmentedControl = nil;
self.segmentedViewControllers = nil;
self.activeViewController = nil;
//[self.segmentedControl release];
[super viewDidUnload];
}
#end
The situation is very similar to that described by this my other question, except that the delegation seems to work fine. I'm providing some more detail about my code. I'm just striping out non-relevant/trivial parts.
ReportScreen.h
#interface ReportScreen : UIViewController <UIImagePickerControllerDelegate, UITextViewDelegate, MBProgressHUDDelegate, SoapDelegate, MapLocationChoiceDelegate>
// ...
#property (nonatomic, retain) MKPointAnnotation *annotation;
#property (nonatomic, retain) IBOutlet UITextView *textView;
#property (nonatomic, retain) IBOutlet UIButton *cameraButton;
#property (nonatomic, retain) IBOutlet UIButton *libraryButton;
#property (nonatomic, retain) IBOutlet UIButton *locationButton;
#property (nonatomic, retain) IBOutlet UIButton *sendButton;
#end
ReportScreen.m
#implementation ReportScreen
#synthesize annotation;
#synthesize textView;
#synthesize cameraButton;
#synthesize libraryButton;
#synthesize locationButton;
#synthesize sendButton;
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Here I used to store the VC's state to a file but it shouldn't be needed now that I'm assigning it as delegate and said delegate seems to still be there even after a memory warning.
}
- (void)viewDidLoad {
[super viewDidLoad];
placeholderText = #"Tell us what's wrong…";
textView.text = placeholderText;
self.annotation = nil;
[self isReadyToSubmit];
hud = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:hud];
hud.delegate = self;
hud.labelText = #"Invio in corso…";
hud.dimBackground = YES;
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Here I used to restore the state of the VC from file but… y'know.
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self isReadyToSubmit];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:#"goToMap"]) {
MapScreen *vc = (MapScreen *)segue.destinationViewController;
// HERE's the magic
vc.mapLocationChoiceDelegate = self;
// MAGIC ends
if(self.annotation != nil) {
vc.annotations = [[NSMutableArray alloc] init];
[vc.annotations addObject:self.annotation];
}
}
}
- (BOOL)isReadyToSubmit {
if(self.annotation != nil) {
locationButton.highlighted = YES;
}
if(![textView.text isEqualToString:placeholderText] && self.annotation != nil) {
[sendButton setEnabled:YES];
} else {
[sendButton setEnabled:NO];
}
return [sendButton isEnabled];
}
- (void)textViewDidBeginEditing:(UITextView *)theTextView {
if([theTextView.text isEqualToString:placeholderText]) {
theTextView.text = #"";
}
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(didFinishEditing:)];
[self.navigationItem setRightBarButtonItem:done animated:YES];
}
- (void)textViewDidEndEditing:(UITextView *)theTextView {
if([theTextView.text isEqualToString:#""]) {
theTextView.text = placeholderText;
}
[self isReadyToSubmit];
}
- (void)didFinishEditing:(id)sender {
[self.navigationItem setRightBarButtonItem:nil animated:YES];
[self.textView resignFirstResponder];
}
// THIS is my delegate protocol's method
- (void)locationChosen:(MKPointAnnotation *)theAnnotation {
self.annotation = theAnnotation;
NSLog(#"R: %#", textView.text);
}
#end
MapScreen.h
#protocol MapLocationChoiceDelegate <NSObject>
- (void)locationChosen:(MKPointAnnotation *)annotation;
#end
// ---
#interface MapScreen : UIViewController <MKMapViewDelegate>
- (void)handleLongPress:(id)sender;
#property (nonatomic, retain) NSMutableArray *annotations;
#property (nonatomic, retain) IBOutlet MKMapView *mapView;
#property (weak) id<MapLocationChoiceDelegate> mapLocationChoiceDelegate;
#end
MapScreen.m
#implementation MapScreen
#synthesize annotations;
#synthesize mapView;
#synthesize mapLocationChoiceDelegate;
- (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.
}
- (void)viewDidLoad {
[super viewDidLoad];
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleLongPress:)];
lpgr.minimumPressDuration = 1.0;
[self.mapView addGestureRecognizer:lpgr];
[mapView addAnnotations:self.annotations];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
#pragma mark - Map handling
- (void)handleLongPress:(id)sender {
if(![sender isKindOfClass:[UILongPressGestureRecognizer class]]) {
return;
}
UILongPressGestureRecognizer *gr = (UILongPressGestureRecognizer *)sender;
if (gr.state != UIGestureRecognizerStateBegan) {
return;
}
CGPoint touchPoint = [gr locationInView:self.mapView];
CLLocationCoordinate2D touchMapCoordinate = [self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = touchMapCoordinate;
self.annotations = [NSMutableArray arrayWithArray:[mapView annotations]];
for(id a in self.annotations) {
if(![a isKindOfClass:[MKUserLocation class]]) {
[mapView removeAnnotation:a];
}
}
[mapView addAnnotation:annotation];
self.annotations = [NSMutableArray arrayWithArray:[mapView annotations]];
// NSDictionary *userInfo = [NSDictionary dictionaryWithObject:annotation forKey:#"annotation"];
// [[NSNotificationCenter defaultCenter] postNotificationName:#"PositionChosen" object:nil userInfo:userInfo];
[self.mapLocationChoiceDelegate locationChosen:annotation];
NSLog(#"M: %#", ((ReportScreen *)self.mapLocationChoiceDelegate).textView.text);
}
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation {
if([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
static NSString *AnnotationIdentifier = #"Annotation";
MKPinAnnotationView* pinView = (MKPinAnnotationView *)[theMapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if (!pinView) {
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
pinView.pinColor = MKPinAnnotationColorRed;
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
} else {
pinView.annotation = annotation;
}
return pinView;
}
#end
The issue is:
ReportScreen pushes (performs segue to, actually) the MapScreen.
If I have some data in the UITextView or if I set some state to the buttons in the ReportScreen and I get a memory warning while the MapScreen is pushed, once I go back to the ReportScreen, all those fields don't show those settings. Apparently textView.text is still set, and so are the states of the buttons, they're just not shown.
Question: why?
In my custom Tab Bar Application, the orientation never seems to change, even if I force rotate the status bar. Here is my code in my AppDelegate:
AppDelegate.h:
#import <UIKit/UIKit.h>
#import "MBProgressHUD.h"
#class exampleViewContoller;
#class example1ViewController;
#class example2ViewController;
#class example3ViewController;
#class example4ViewController;
#interface <appname>AppDelegate : NSObject <UIApplicationDelegate, MBProgressHUDDelegate> {
UIWindow *window;
UITabBarController *rootController;
exampleViewContoller *viewController;
example1ViewController *viewController1;
example2ViewController *viewController2;
example3ViewController *viewController3;
example4ViewController *viewController4;
NSMutableData *responseData;
NSMutableArray *tweets;
MBProgressHUD *HUD;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *rootController;
#property (nonatomic, retain) IBOutlet exampleViewContoller *viewController;
#property (nonatomic, retain) IBOutlet example1ViewController *viewController1;
#property (nonatomic, retain) IBOutlet example2ViewController *viewController2;
#property (nonatomic, retain) IBOutlet example3ViewController *viewController3;
#property (nonatomic, retain) IBOutlet example4ViewController *viewController4;
#property (nonatomic, retain) NSMutableArray *tweets;
#end
AppDelegate.m:
#import "<appname>AppDelegate.h"
#import "exampleViewContoller.h"
#import "example1ViewController.h"
#import "example2ViewController.h"
#import "example3ViewController.h"
#import "example4ViewController.h"
#import "SBJson.h"
#define TMP NSTemporaryDirectory()
#implementation <appname>AppDelegate
#synthesize window = _window;
#synthesize rootController;
#synthesize viewController;
#synthesize viewController1;
#synthesize viewController2;
#synthesize viewController3;
#synthesize viewController4;
#synthesize tweets;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CGFloat width = self.rootController.view.bounds.size.width;
CGFloat height = self.rootController.view.bounds.size.height;
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, width, height)];
UIImage *imageView = [UIImage imageNamed:#"TabBarBackground.png"];
UIColor *kMainColor = [[UIColor alloc] initWithPatternImage:imageView];
[v setBackgroundColor:kMainColor];
[kMainColor release];
[self.rootController.tabBar insertSubview:v atIndex:0];
[imageView release];
[v release];
responseData = [[NSMutableData data] retain];
tweets = [NSMutableArray array];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:#"http://api.twitter.com/1/statuses/user_timeline.json?screen_name=ENTER_USER_HERE&count=20"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
NSAssert(nil != self.rootController, #"tab bar controller not hooked up!");
BOOL iPad = NO;
#ifdef UI_USER_INTERFACE_IDIOM
iPad = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
#endif
if (iPad) {
self.viewController = [[[exampleViewContoller alloc] initWithNibName:#"exampleViewController_iPad" bundle:nil] autorelease];
self.viewController1 = [[example1ViewController alloc] initWithNibName:#"example1ViewController_iPad" bundle:nil] autorelease];
self.viewController2 = [[[example2ViewController alloc] initWithNibName:#"example2ViewController_iPad" bundle:nil] autorelease];
self.viewController3 = [[[example3ViewController alloc] initWithNibName:#"example3ViewController_iPad" bundle:nil] autorelease];
self.viewController4 = [[[example4ViewController alloc] initWithNibName:#"example4ViewController_iPad" bundle:nil] autorelease];
} else {
self.viewController = [[[exampleViewContoller alloc] initWithNibName:#"exampleViewContoller_iPhone" bundle:nil] autorelease];
self.viewController1 = [[[example1ViewController alloc] initWithNibName:#"example1ViewController_iPhone" bundle:nil] autorelease];
self.viewController2 = [[[example2ViewController alloc] initWithNibName:#"example2ViewController2_iPhone" bundle:nil] autorelease];
self.viewController3 = [[[example3ViewController alloc] initWithNibName:#"example3ViewController_iPhone" bundle:nil] autorelease];
self.viewController4 = [[[example4ViewController alloc] initWithNibName:#"example4ViewController_iPhone" bundle:nil] autorelease];
}
self.rootController.viewControllers = [NSArray arrayWithObjects:self.viewController, self.viewController4, self.viewController1, self.viewController3, self.viewController2, nil];
[viewController release];
[viewController1 release];
[viewController2 release];
[viewController3 release];
[viewController4 release];
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
self.window.rootViewController = self.rootController;
#else
[self.window addSubview:rootController.view];
#endif
[self.window makeKeyAndVisible];
HUD = [[MBProgressHUD alloc] initWithView:viewController.view];
[viewController.view addSubview:HUD];
[HUD show:NO];
HUD.delegate = self;
HUD.labelText = #"Loading";
return YES;
}
//[---CODE CLIP---]
- (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation {
CGFloat width = self.rootController.view.bounds.size.width*2;
CGFloat height = self.rootController.view.bounds.size.height;
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, width, height)];
UIImage *imageView = [UIImage imageNamed:#"TabBarBackground.png"];
UIColor *kMainColor = [[UIColor alloc] initWithPatternImage:imageView];
[v setBackgroundColor:kMainColor];
[kMainColor release];
[self.rootController.tabBar insertSubview:v atIndex:0];
[imageView release];
[v release];
}
- (void)hudWasHidden {
[HUD removeFromSuperview];
}
//[---CODE CLIP---]
- (void)dealloc
{
[_window release];
[rootController release];
[HUD release];
[super dealloc];
}
#end
The problem is that when I rotate the device in iOS Simulator, the application won't rotate. Any ideas would be much appreciated!
UPDATE
I have also noticed that the launch image is also not rotating (for iPad that is - iPhone doesn't do landscape launch images).
NOTE FOR JMANS
I overrode UITabBarController:
#implementation UITabBarController (MyApp)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
if(self.selectedIndex==4)
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
else
return (toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
I would start by including this in all of your view controllers:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
Also, make sure that you are supporting multiple orientations in your Info.plist.
Tab Bar Controllers and View Rotation
Tab bar controllers support a portrait orientation by default and do not rotate to a landscape orientation unless all of the contained view controllers support such an orientation. When a device orientation change occurs, the tab bar controller queries its array of view controllers. If any one of them does not support the orientation, the tab bar controller does not change its orientation.
(I'm beginner.)
I'm practicing Navigation Controller. I try to implement a simple calculator.
I ran the code in simulator. After I pressed any button which was linked to "addFunction", "substractFunction", "multiplyFunction" or "divideFunction", It crashed.
The debugger marked the following code in main.m
int retVal = UIApplicationMain(argc, argv, nil, nil);
and said "Thread 1: Program received signal: "SIGABRT"."
Does anyone know how to cope with this situation? Thanks.
Here's the code:
ChangeAppView.h:
#import <UIKit/UIKit.h>
#class ChangeViewController;
#interface ChangeAppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
UINavigationController *navigationController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) UINavigationController *navigationController;
#end
ChangeAppDelegate.m:
#import "ChangeAppDelegate.h"
#import "ChangeViewController.h"
#implementation ChangeAppDelegate
#synthesize window;
#synthesize navigationController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
navigationController = [[UINavigationController alloc] init];
self.window.rootViewController = navigationController;
ChangeViewController *changeViewController = [[ChangeViewController alloc] initWithNibName:#"ChangeViewController" bundle:nil];
[navigationController pushViewController:changeViewController animated:YES];
[changeViewController release];
[self.window makeKeyAndVisible];
return YES;
}
…
- (void)dealloc
{
[navigationController release];
[window release];
[super dealloc];
}
#end
CalculatorViewController.h:
#import <UIKit/UIKit.h>
#interface CalculatorViewController : UIViewController
{
IBOutlet UITextField *numberField1;
IBOutlet UITextField *numberField2;
IBOutlet UILabel *resultLabel;
}
#property (nonatomic , retain) IBOutlet UITextField *numberField1;
#property (nonatomic , retain) IBOutlet UITextField *numberField2;
#property (nonatomic , retain) IBOutlet UILabel *resultLabel;
-(IBAction)addFunction:(id)sender;
-(IBAction)substractFunction:(id)sender;
-(IBAction)multiplyFunction:(id)sender;
-(IBAction)divideFunction:(id)sender;
-(IBAction)clear:(id)sender;
-(IBAction)backgroundTap:(id)sender;
#end
CalculatorViewController.m:
#import "CalculatorViewController.h"
#implementation CalculatorViewController
#synthesize numberField1;
#synthesize numberField2;
#synthesize resultLabel;
-(IBAction)addFunction:(id)sender
{
float a = ([numberField1.text floatValue]);
float b = ([numberField2.text floatValue]);
resultLabel.text = [NSString stringWithFormat:#"%2.f" , a+b];
}
-(IBAction)substractFunction:(id)sender
{
float a = ([numberField1.text floatValue]);
float b = ([numberField2.text floatValue]);
NSString *result = [[NSString alloc] initWithFormat:#"%2.f" , a-b];
resultLabel.text = result;
[result release];
}
-(IBAction)multiplyFunction:(id)sender
{
float a = ([numberField1.text floatValue]);
float b = ([numberField2.text floatValue]);
resultLabel.text = [[NSString alloc] initWithFormat:#"%2.f" , a*b];
}
-(IBAction)divideFunction:(id)sender
{
float a = ([numberField1.text floatValue]);
float b = ([numberField2.text floatValue]);
resultLabel.text = [[NSString alloc] initWithFormat:#"%2.3f" , a/b];
}
-(IBAction)clear:(id)sender
{
numberField1.text = #"";
numberField2.text = #"";
resultLabel.text = #"";
}
-(IBAction)backgroundTap:(id)sender
{
[numberField1 resignFirstResponder];
[numberField2 resignFirstResponder];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[numberField1 release];
[numberField2 release];
[resultLabel release];
[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.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
self.title = #"Calculator";
[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
From the exception above, it seems that your IBActions are not properly connected.
As dasdom mentioned, delete all your buttons, create new buttons and then add IBAction methods accordingly.
Also one more thing i recognized in your code, in the multiply and divide methods there is a memory leak.You have written
resultLabel.text = [[NSString alloc] initWithFormat:#"%2.f" , a*b];
it should be
resultLabel.text = [[[NSString alloc] initWithFormat:#"%2.f" , a*b]autorelease];
or
resultLabel.text = [NSString StringWithFormat:#"%2.f" , a*b];
and do a similar change in divide method also.
To what did you link your backgroundtap method?
It seems that you buttons aren't buttons. They seem to be view. Delete the buttons from you nib and put new buttons there and link them you our IBActions.
NEW CODE
DatePickerViewController.h
#import <UIKit/UIKit.h>
#protocol DatePickerViewControllerDelegate;
#interface DatePickerViewController : UIViewController {
IBOutlet UIDatePicker *datePicker;
id<DatePickerViewControllerDelegate> delegate;
}
#property (retain) IBOutlet UIDatePicker *datePicker;
#property (assign) id<DatePickerViewControllerDelegate> delegate;
NSInteger buttonPressed;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;
- (IBAction)doneButtonPressed:(id)sender;
#end
#protocol DatePickerViewControllerDelegate <NSObject>
#optional
-(void)datePickerViewController:(DatePickerViewController *)controller didChooseDate:(NSString *)chosenDate;
#end
DatePickerViewController.m
#import "DatePickerViewController.h"
#implementation DatePickerViewController
#synthesize datePicker, delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
self.title = #"Date Picker";
}
return self;
}
- (void)viewDidLoad {
NSLog(#"Date Picker. viewDidLoad");
[super viewDidLoad];
double days = 2.0f;
datePicker.date = [NSDate dateWithTimeIntervalSinceNow:60.0f * 60.0f * 24.0f * days];
}
//-(void)datePickerViewController:(DatePickerViewController *)controller didChooseDate:(NSString *)chosenDate;
- (IBAction)doneButtonPressed:(id)sender
{
if ([self.delegate respondsToSelector:#selector(datePickerViewController:didChooseDate:)]) {
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSString *dateString = [dateFormatter stringFromDate:[datePicker date]];
[self.delegate datePickerViewController:self didChooseDate:dateString];
[self dismissModalViewControllerAnimated:YES];
}
}
- (void)dealloc {
[datePicker release];
[super dealloc];
}
#end
DatePickerViewController2.h
#import <UIKit/UIKit.h>
#protocol DatePickerViewController2Delegate;
#interface DatePickerViewController2 : UIViewController {
IBOutlet UIDatePicker *datePicker2;
id<DatePickerViewController2Delegate> delegate;
}
#property (retain) IBOutlet UIDatePicker *datePicker2;
#property (assign) id<DatePickerViewController2Delegate> delegate;
NSInteger buttonPressed2;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;
- (IBAction)doneButtonPressed2:(id)sender;
#end
#protocol DatePickerViewController2Delegate <NSObject>
#optional
-(void)datePickerViewController2:(DatePickerViewController2 *)controller didChooseDate:(NSString *)chosenDate;
#end
DatePickerViewController2.m
#import "DatePickerViewController2.h"
#implementation DatePickerViewController2
#synthesize datePicker2, delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
self.title = #"Date Picker2";
}
return self;
}
- (void)viewDidLoad {
NSLog(#"Date Picker2. viewDidLoad");
[super viewDidLoad];
double days = 2.0f;
datePicker2.date = [NSDate dateWithTimeIntervalSinceNow:60.0f * 60.0f * 24.0f * days];
}
//-(void)datePickerViewController:(DatePickerViewController *)controller didChooseDate:(NSString *)chosenDate;
- (IBAction)doneButtonPressed2:(id)sender
{
if ([self.delegate respondsToSelector:#selector(datePickerViewController2:didChooseDate:)]) {
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSString *dateString = [dateFormatter stringFromDate:[datePicker2 date]];
[self.delegate datePickerViewController2:self didChooseDate:dateString];
[self dismissModalViewControllerAnimated:YES];
}
}
- (void)dealloc {
[datePicker2 release];
[super dealloc];
}
#end
DatePickerModalExampleAppDelegate.h
#import <UIKit/UIKit.h>
#class DatePickerModalExampleViewController;
#interface DatePickerModalExampleAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
DatePickerModalExampleViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet DatePickerModalExampleViewController *viewController;
#end
DatePickerModalExampleAppDelegate.m
#import "DatePickerModalExampleAppDelegate.h"
#import "DatePickerModalExampleViewController.h"
#implementation DatePickerModalExampleAppDelegate
#synthesize window;
#synthesize viewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
#end
DatePickerModalExampleViewController.h
#import <UIKit/UIKit.h>
#import "DatePickerViewController.h"
#import "DatePickerViewController2.h"
#interface DatePickerModalExampleViewController : UIViewController <DatePickerViewControllerDelegate> {
IBOutlet UIButton *button;
IBOutlet UIButton *button2;
IBOutlet UIButton *button3;
}
#property(nonatomic, retain) IBOutlet UIButton *button;
#property(nonatomic, retain) IBOutlet UIButton *button2;
#property(nonatomic, retain) IBOutlet UIButton *button3;
-(IBAction)buttonPressed:(id)sender;
-(IBAction)buttonPressed2:(id)sender;
#end
DatePickerModalExampleViewController.m
#import "DatePickerModalExampleViewController.h"
#implementation DatePickerModalExampleViewController
#synthesize button;
#synthesize button2;
#synthesize button3;
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
-(IBAction)buttonPressed:(id)sender{
NSLog(#"I was pressed");
buttonPressed = ((UIButton *)sender).tag;
DatePickerViewController *datePickerViewController = [[DatePickerViewController alloc] initWithNibName:#"DatePickerViewController" bundle:nil];
datePickerViewController.delegate = self;
[self presentModalViewController:datePickerViewController animated:YES];
[datePickerViewController release];
switch (((UIButton*)sender).tag)
{
case 100001:
NSLog(#"Button 1 was pressed");
//some code
break;
case 100002:
NSLog(#"Button 2 was pressed");
//some code
break;
}
}
-(IBAction)buttonPressed2:(id)sender{
NSLog(#"I was pressed2");
buttonPressed2 = ((UIButton *)sender).tag;
DatePickerViewController2 *datePickerViewController2 = [[DatePickerViewController2 alloc] initWithNibName:#"DatePickerViewController2" bundle:nil];
datePickerViewController2.delegate = self;
[self presentModalViewController:datePickerViewController2 animated:YES];
[datePickerViewController2 release];
switch (((UIButton*)sender).tag)
{
case 100003:
NSLog(#"Button 3 was pressed");
//some code
break;
}
}
-(void)viewDidLoad{
[super viewDidLoad];
self.button.tag = 100001;
self.button2.tag = 100002;
self.button3.tag = 100003;
buttonPressed = -1;
buttonPressed2 = -1;
}
-(void)datePickerViewController:(DatePickerViewController *)controller didChooseDate:(NSString *)chosenDate{
NSLog(#"Chosen Date as String: %#", chosenDate );
if (buttonPressed == -1)
return;
UIButton *buttonToSet = (UIButton*)[self.view viewWithTag:buttonPressed];
buttonPressed = -1;
[buttonToSet setTitle: chosenDate forState: UIControlStateNormal];
[self dismissModalViewControllerAnimated:YES];
}
-(void)datePickerViewController2:(DatePickerViewController2 *)controller didChooseDate:(NSString *)chosenDate{
NSLog(#"Chosen Date as String: %#", chosenDate );
if (buttonPressed2 == -1)
return;
UIButton *buttonToSet = (UIButton*)[self.view viewWithTag:buttonPressed2];
buttonPressed2 = -1;
[buttonToSet setTitle: chosenDate forState: UIControlStateNormal];
[self dismissModalViewControllerAnimated:YES];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (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.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[button3 release];
[button2 release];
[button release];
[super dealloc];
}
#end
Hello you can set the property "tag" on button to identify it
Uncomment
- (void)viewDidLoad {
[super viewDidLoad];
}
And type :
- (void)viewDidLoad {
[super viewDidLoad];
self.button.tag = 100001;
self.button2.tag = 100002;
}
then
turn in DatePickerModalExampleViewController.h
(IBAction)buttonPressed to (IBAction)buttonPressed:(id)sender;
Then turn in DatePickerModalExampleViewController.m
-(IBAction)buttonPressed to -(IBAction)buttonPressed:(id)sender
and in DatePickerModalExampleViewController.m this method could be like that :
-(IBAction)buttonPressed:(id)sender
{
switch(((UIButton*)sender).tag)
{
case 100001
NSLog(#"Button 1 was pressed");
DatePickerViewController *datePickerViewController =[DatePickerViewController alloc] initWithNibName:#"DatePickerViewController" bundle:nil];
datePickerViewController.delegate = self;
[self presentModalViewController:datePickerViewController animated:YES];
[datePickerViewController release];
break;
case 100002 :
NSLog(#"Button 2 was pressed");
// some code
break;
}
}
As you use the interfacebuilder I'm not friend with it but you must relink your actions
I'm writting from my PC so without xcode some code can contains syntax error be carreful. but it's the main idea.
Use tag for both buttons and check the condition accordingly...