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
Related
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........
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?
I having a problem with a Bad Exception that I could not locate at first, but now have it pinned down on a [super dealloc];, but I have no idea why this happens.
Here is my code :
EditingViewController.h
#interface EditingViewController : UIViewController
{
NSManagedObject *editedObject;
NSString *editedFieldKey;
NSString *editedFieldName;
}
#property (nonatomic, retain) NSManagedObject *editedObject;
#property (nonatomic, retain) NSString *editedFieldKey;
#property (nonatomic, retain) NSString *editedFieldName;
- (IBAction)cancel;
- (IBAction)save;
#end
EditingViewController.m
#import "EditingViewController.h"
#implementation EditingViewController
#synthesize editedObject, editedFieldKey, editedFieldName;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad
{
self.title = NSLocalizedString(editedFieldName, nil);
// Configure the save and cancel buttons.
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:#selector(save)];
self.navigationItem.rightBarButtonItem = saveButton;
[saveButton release];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancel)];
self.navigationItem.leftBarButtonItem = cancelButton;
[cancelButton release];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (IBAction)save
{
[self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)cancel
{
// Don't pass current value to the edited object, just pop.
[self.navigationController popViewControllerAnimated:YES];
}
- (void)dealloc
{
[editedObject release];
[editedFieldKey release];
[editedFieldName release];
//[super dealloc];
}
#end
As you can see, I commented the [super dealloc];, which is causing the Bad Exception, but this is obviously not a good solution.
Any idea what I am doing wrong ?
Thanks
The properties might have never been used, so they haven't been initialized in any way
- (void)dealloc
{
self.editedObject = nil;
self.editedFieldKey = nil;
self.editedFieldName = nil;
[super dealloc];
}
Is there something that I need to remember when using the windows-based template? Because I'm unclear as to why the tabs are showing up but nothing in the views are showing up.
Could you help? Because I've been searching through previous questions for a few hours now and I still haven't found anything to clear this up.
AnotherMadeUpAppDelegate.h
#import <UIKit/UIKit.h>
#import "AnotherMadeUpViewController.h"
#interface AnotherMadeUpAppDelegate : NSObject <UIApplicationDelegate> {
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
AnotherMadeUpAppDelegate.m
#import "AnotherMadeUpAppDelegate.h"
#implementation AnotherMadeUpAppDelegate
#synthesize window=_window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UIViewController *vc1 = [[UIViewController alloc] init];
UIViewController *vc2 = [[UIViewController alloc] init];
AnotherMadeUpViewController *vc3 = [[AnotherMadeUpViewController alloc] init];
UITabBarController *tbc = [[UITabBarController alloc] init];
tbc.viewControllers = [NSArray arrayWithObjects:vc1, vc2, vc3, nil];
[vc1 release];
[vc2 release];
[vc3 release];
[self.window addSubview:tbc.view];
[self.window makeKeyAndVisible];
return YES;
}
...
#end
AnotherMadeUpViewController.h
#import <UIKit/UIKit.h>
#interface AnotherMadeUpViewController : UIViewController<UIScrollViewDelegate>
{
IBOutlet UIPageControl *pageControl;
IBOutlet UIScrollView *scroller;
IBOutlet UILabel *label;
}
#property (nonatomic,retain)IBOutlet UIPageControl *pageControl;
#property (nonatomic,retain)IBOutlet UIScrollView *scroller;
#property (nonatomic,retain)IBOutlet UILabel *label;
-(IBAction)clickPageControl:(id)sender;
#end
AnotherMadeUpViewController.m
#import "AnotherMadeUpViewController.h"
#implementation AnotherMadeUpViewController
#synthesize pageControl,scroller,label;
-(IBAction)clickPageControl:(id)sender
{
int page=pageControl.currentPage;
CGRect frame=scroller.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[scroller scrollRectToVisible:frame animated:YES];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
int page = scrollView.contentOffset.x/scrollView.frame.size.width;
pageControl.currentPage=page;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[super dealloc];
[label release];
}
- (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];
scroller.pagingEnabled=YES;
CGFloat labelOriginX = label.frame.origin.x;
CGFloat labelOriginY = label.frame.origin.y;
CGFloat scrollWidth = 0;
int pageNumber = 0;
for (int i=0; i<9; i++)
{
CGRect rect = label.frame;
rect.size.height = label.frame.size.height;
rect.size.width = label.frame.size.width;
rect.origin.x = labelOriginX + scrollWidth;
rect.origin.y = labelOriginY;
label.frame = rect;
label.text = [NSString stringWithFormat:#"%d", pageNumber];
label.textColor = [UIColor redColor];
[scroller addSubview:label];
pageNumber++;
scrollWidth += scroller.frame.size.width;
}
scroller.delegate=self;
scroller.directionalLockEnabled=YES;
scroller.showsHorizontalScrollIndicator=NO;
scroller.showsVerticalScrollIndicator=NO;
pageControl.numberOfPages=9;
pageControl.currentPage=0;
scroller.contentSize=CGSizeMake(pageControl.numberOfPages*self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:scroller];
}
- (void)viewDidUnload
{
[super viewDidUnload];
[label release];
self.label = nil;
// 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
Dissecting your viewDidLoad –
scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
You seem to be creating a new scroll view instance here but you have declared it as an outlet. If you don't have an outlet then remove the IBOutlet tag for scroller. If you do have one and want to use it then remove the above line. A shorter way of doing it would be,
scroller = [[UIScrollView alloc] initWithFrame:self.view.bounds];
Another thing is that you are creating 10 labels but assigning no frame. To show one of them each in different page,
int pageNumber = 0;
for (int i = 0; i < 10; i++)
{
UILabel *label = [[UILabel alloc] init];
[label sizeToFit];
label.center = CGPointMake (((2 * i + 1) * self.view.frame.size.width) / 2, self.view.frame.size.height / 2);
label.text = [NSString stringWithFormat:#"%d", pageNumber];
[scroller addSubview:label];
[label release];
pageNumber++;
}
and later set the contentSize to show 10 pages,
scroller.contentSize = CGSizeMake(10 * self.view.frame.size.width, self.view.frame.size.height);
The problem is with this line
AnotherMadeUpViewController *vc3 = [[AnotherMadeUpViewController alloc] init];
You need to change it to
AnotherMadeUpViewController *vc3 = [[AnotherMadeUpViewController alloc] initWithNibName:#"AnotherMadeUpViewController" bundle:nil];
Then your .xib will get loaded and your outlets will be connected.
And don't forget to connect your outlets to File's owner in IB.
Well, i have 2 view controllers the fist has three buttons each one represents an image on an array of images. When a user presses a button moves on the second view controller which has just an IBOutlet UIImageView to view the image. How can i do this programming trick using the UIImagePickerController? here are my files:
// PhotoListViewController.h
#import <UIKit/UIKit.h>
#import "PhotoDetailViewController.h"
#interface PhotoListViewController : UIViewController {
IBOutlet UIButton *button;
IBOutlet UIImageView *image1;
IBOutlet UIImageView *image2;
IBOutlet UIImageView *image3;
IBOutlet UILabel *label1;
IBOutlet UILabel *label2;
IBOutlet UILabel *label3;
IBOutlet UILabel *nameMikro1;
IBOutlet UILabel *nameMikro2;
IBOutlet UILabel *nameMikro3;
NSString *nameMikroProp;
IBOutlet PhotoDetailViewController *photoDetailViewController;
}
#property (nonatomic, retain) PhotoDetailViewController *photoDetailViewController;
-(IBAction)showImage:(id)sender;
#property (copy) NSString *nameMikroProp;
#end
// PhotoListViewController.m
#import "PhotoListViewController.h"
#import "PhotoDetailViewController.h"
#implementation PhotoListViewController
#synthesize nameMikroProp;
#synthesize photoDetailViewController;
-(IBAction)showImage:(id)sender{
photoDetailViewController = [[PhotoDetailViewController alloc]init];
//photoDetailViewController.delegate = self;
if([sender tag] == 4){
//[photoDetailViewController.imageViewprop setImage:[UIImage imageNamed:#"zaab.png"]];
//[self presentModalViewController:self.imgPicker animated:YES];
}
else if([sender tag] == 5){
}
else{
}
[self.navigationController pushViewController:photoDetailViewController animated:YES];
[photoDetailViewController release];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
NSMutableArray *imageArray = [[NSMutableArray alloc] init];
[imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"main" ofType:#"jpg"]]];
[imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"main2" ofType:#"jpg"]]];
[imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"main3" ofType:#"jpg"]]];
NSMutableArray *nameArray = [[NSMutableArray alloc] init];
[nameArray addObject:#"zaab's photo one"];
[nameArray addObject:#"zaab's photo two"];
[nameArray addObject:#"zaab's photo three"];
nameMikro1.text = nameMikroProp;
nameMikro2.text = nameMikroProp;
nameMikro3.text = nameMikroProp;
if([#"zaab" isEqualToString:nameMikro1.text]) {
[image1 setImage:[imageArray objectAtIndex:0]];
[image2 setImage:[imageArray objectAtIndex:1]];
[image3 setImage:[imageArray objectAtIndex:2]];
[label1 setText:[nameArray objectAtIndex:0]];
[label2 setText:[nameArray objectAtIndex:1]];
[label3 setText:[nameArray objectAtIndex:2]];
}
else if([#"evza" isEqualToString:nameMikro1.text]) {
[image1 setImage:[UIImage imageNamed:#"evza.jpg"]];
}
[imageArray release];
[nameArray release];
[super viewDidLoad];
}
- (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 {
[nameMikro1 release];
[nameMikro2 release];
[nameMikro3 release];
[label1 release];
[label2 release];
[label3 release];
[image1 release];
[image2 release];
[image3 release];
[super dealloc];
}
#end
// PhotoDetailViewController.h
#import <UIKit/UIKit.h>
#interface PhotoDetailViewController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate> {
IBOutlet UIImageView *imageViewprop;
UIImagePickerController *imgPicker;
}
#property (nonatomic, retain) UIImageView *imageViewprop;
#property (nonatomic, retain) UIImagePickerController *imgPicker;
- (IBAction)grabImage;
#end
// PhotoDetailViewController.m
#import "PhotoDetailViewController.h"
#implementation PhotoDetailViewController
#synthesize imageViewprop;
#synthesize imgPicker;
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
imageViewprop.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"main" ofType:#"jpg"]];
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
- (IBAction)grabImage {
[self presentModalViewController:self.imgPicker animated:YES];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.allowsImageEditing = NO;
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
- (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 {
[super dealloc];
}
#end
Why do you need an image picker controller to show an image? ImagePickerController is used to pick an image from the album or by using camera.
What you can do is to pass the image from the PhotoListViewController to PhotoDetailViewController.
In the PhotoDetailViewController, you can have a property of UIImage (synthesized), so that you can set it before loading it. Then in the PhotoDetailVC use a UIImageView to show the image.
If you are looking for more advance functionality with image, look at the three20 framework at
three20 at github.