Cannot get requestWhenInUseAuthorization or requestWhenInUseAuthorization to work - xcode6

This is my code, it works fine in IOS7 but in IOS8 I can't get authorisation alert to show. I already added the keys in the Info plist for NSLocationWhenInUseUsageDescription & NSLocationAlwaysUsageDescription.
.h
#import <CoreLocation/CoreLocation.h>
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *LatitudeLabel;
#property (weak, nonatomic) IBOutlet UILabel *LongitudeLabel;
#property (weak, nonatomic) IBOutlet UILabel *GPSAccuracyLabel;
#property (weak, nonatomic) IBOutlet UILabel *AltitudeLabel;
#property (weak, nonatomic) IBOutlet UILabel *VerticalAccuracyLabel;
- (IBAction)getCurrentLocation:(id)sender;
#end
.m
#import "ViewController.h"
#interface ViewController ()
// create a property to hold current time.
#property (nonatomic, strong) NSDate *lastUpdateTime;
#end
#implementation ViewController {
CLLocationManager *locationManager;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.lastUpdateTime = [NSDate date];
locationManager = [[CLLocationManager alloc] init];
CLAuthorizationStatus authorizationStatus= [CLLocationManager authorizationStatus];
if (authorizationStatus == kCLAuthorizationStatusAuthorizedAlways ||
authorizationStatus == kCLAuthorizationStatusAuthorizedWhenInUse) {
[locationManager startUpdatingLocation];
}
}

For example if you need "WhenInUseAuthorization" in your viewDidLoad :
-(void)viewDidLoad {
[super viewDidLoad];
self.lastUpdateTime = [NSDate date];
locationManager = [[CLLocationManager alloc] init];
CLAuthorizationStatus authorizationStatus= [CLLocationManager authorizationStatus];
NSLog(#"authStatus = %d",authorizationStatus);
if([locationManger respondsToSelector:#selector(requestWhenInUseAuthorization)])
[locationManger requestWhenInUseAuthorization];
[locationManager startUpdatingLocation];
}
Check your status from authStatus
typedef enum {
kCLAuthorizationStatusNotDetermined = 0,
kCLAuthorizationStatusRestricted ,
kCLAuthorizationStatusDenied ,
kCLAuthorizationStatusAuthorized ,
kCLAuthorizationStatusAuthorizedAlways = kCLAuthorizationStatusAuthorized ,
kCLAuthorizationStatusAuthorizedWhenInUse
} CLAuthorizationStatus;
And remember to add in your info.plist
<key>NSLocationWhenInUseUsageDescription</key>
<string>Your message</string>

This is my Info.Plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>com.nicoll55.$(PRODUCT_NAME:rfc1034identifier)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Required to update your location in the background.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Required to obtain your location.</string>
<key>UIApplicationExitsOnSuspend</key>
<false/>
</dict>
</plist>

Related

IBAction Method not being called when button is pushed. But View controller is being pushed

I am attempting to pass information from one viewController to another using a push segue with an IBAction button named *line. However from what I can tell this method is not being called and the NSLog(#"%#", see); I inserted to test the method is not displaying any message. Here is some code for the first viewController.
DetailController.m
#import "DetailController.h"
#import "City.h"
#import "ViewController.h"
#import "VideoController.h"
#import "Helper.h"
#interface DetailController ()
#property (nonatomic, strong) IBOutlet VideoController *videoViewController;
#end
#implementation DetailController
#synthesize city, ClubName, Price, Vip, Promo, remain,p,deal,money,camera,tweet,post;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
self.videoViewController = [[VideoController alloc] init];
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320,1400)];
[super viewDidLoad];
UIImage *highlightedButtonImage = [UIImage imageNamed:#"twitter.png"];
UIImage *highlightedButtonImage2 = [UIImage imageNamed:#"twitter2.png"];
[Helper customizeBarButton:self.tweet image:highlightedButtonImage highlightedImage:highlightedButtonImage2];
UIImage *faceButtonImage = [UIImage imageNamed:#"facebook.png"];
UIImage *faceButtonImage2 = [UIImage imageNamed:#"facebook2.png"];
[Helper customizeBarButton:self.post image:faceButtonImage highlightedImage:faceButtonImage2];
UIImage *taxiButtonImage = [UIImage imageNamed:#"taxi.png"];
UIImage *taxiButtonImage2 = [UIImage imageNamed:#"taxi2.png"];
[Helper customizeBarButton:self.taxi image:taxiButtonImage highlightedImage:taxiButtonImage2];
// Do any additional setup after loading the view.
UIFont *labelFont=[UIFont fontWithName:#"King Kikapu" size:15.0];
UIFont *myFont=[UIFont fontWithName:#"Deutsch Gothic" size:20.0];
UIFont *myFont2=[UIFont fontWithName:#"Deutsch Gothic" size:35.0];
UIFont *titleFont=[UIFont fontWithName:#"Pornstar" size:50.0];
NSString * name= self.city.clubName;
NSString * line= self.city.clubLine;
NSString * description= self.city.promo;
NSString * price= self.city.price;
NSString *ipCam= self.city.camera;
remain.font=labelFont;
remain.text=#"VIP Remaining :";
p.font=labelFont;
p.text=#"Price :";
money.font=myFont;
deal.font=labelFont;
deal.text=#"Promotions :";
ClubName.font=titleFont;
ClubName.text=name;
Vip.font=myFont2;
Vip.text=line;
Price.font=myFont2;
Price.text=price;
Promo.font=myFont;
Promo.text=description;
}
- (IBAction)PostFacebook:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController * facebook= [[SLComposeViewController alloc]init];
facebook= [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[facebook setInitialText:[NSString stringWithFormat:#"I'm heading to"]];
[self presentViewController:facebook animated:YES completion:nil];
[facebook setCompletionHandler:^(SLComposeViewControllerResult result){
NSString * output;
switch (result) {
case SLComposeViewControllerResultCancelled:
output=#"Action Cancelled";
break;
case SLComposeViewControllerResultDone:
output=#"Post Succesful";
default:
break;
}
UIAlertView *alert= [[UIAlertView alloc]initWithTitle:#"Facebook" message:output delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
[alert show];
}];
}}
- (IBAction)PostTwitter:(id)sender {
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet= [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:#"I'm heading to"];
[self presentViewController: tweetSheet animated:YES completion:nil];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)line:(id)sender {
NSString *see=self.city.camera;
NSLog(#"%#", see);
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard"
bundle:nil];
self.videoViewController = [storyboard instantiateViewControllerWithIdentifier:#"Page3"];
self.videoViewController.city.camera= self.city.camera;
[self.navigationController pushViewController:self.videoViewController animated:YES];
}
#end
As a test I inserted NSLog(#"%#", see); into the IBAction method but this is not returning any value
DetailController.h
#import <UIKit/UIKit.h>
#import "VideoController.h"
#import <Social/Social.h>
#class City;
#interface DetailController : UIViewController {
IBOutlet UIScrollView *scroller;
}
#property (weak, nonatomic) IBOutlet UIBarButtonItem *taxi;
#property (strong,nonatomic) City *city;
#property (weak, nonatomic) IBOutlet UILabel *ClubName;
#property (weak, nonatomic) IBOutlet UILabel *Vip;
#property (weak, nonatomic) IBOutlet UILabel *Price;
#property (nonatomic, strong)NSString * camera;
#property (weak, nonatomic) IBOutlet UILabel *Promo;
#property (weak, nonatomic) IBOutlet UILabel *remain;
#property (weak, nonatomic) IBOutlet UILabel *p;
#property (weak, nonatomic) IBOutlet UILabel *deal;
#property (weak, nonatomic) IBOutlet UILabel *money;
#property (weak, nonatomic) IBOutlet UIBarButtonItem *tweet;
#property (weak, nonatomic) IBOutlet UIBarButtonItem *post;
- (IBAction)line:(id)sender;
#end
Thanks for any suggestions on why this method is not being called when the button is pushed
The only way for your IBAction to NOT get called is when the button is not appropriately connected to it. Double check that in the connections inspector.

Passing map-data between ViewController

I nearly followed this post. The viewcontroller will push via storyboard. Here the relevant code:
ViewController.m:
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(#"%#", view.annotation.title);
MapPoint *annView = view.annotation;
DetailViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailViewController"];
dvc.title = #"my own Details";
dvc.titleTMP = annView.title;
dvc.subtitleTMP = annView.subtitle;
[self.navigationController pushViewController:dvc animated:YES];
}
MapPoint.h:
#interface MapPoint : NSObject <MKAnnotation>
{
NSString *_title;
NSString *_subtitle;
CLLocationCoordinate2D _coordinate;
UIImage *_storeImage;
}
#property (copy) NSString *title;
#property (copy) NSString *subtitle;
#property (nonatomic, readonly) UIImage *storeImage;
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithTitle:(NSString*)title subtitle:(NSString*)subtitle coordinate:(CLLocationCoordinate2D)coordinate storeImage:(UIImage*)storeImage;
and DetailViewController:
#interface DetailViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIImageView *branchImage;
#property (strong, nonatomic) IBOutlet UILabel *titleLabel;
#property (strong, nonatomic) IBOutlet UITextView *textView;
I think my mistake is in the calloutAccessoryControlTapped method but I dont know what is the real reason for this issue.
If you use a storyboard, can use prepareForSegue method and set within a class similarly as you have made.
I post a portion of code
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"Brs"]){
NSLog(#"segue %# ",[segue identifier]);
[segue.destinationViewController setPath:[ris_xml objectAtIndex:index_post-1] ];
}
}
In this example I set attribute "Path" of next UIViewController only if his identifier is "Brs".
For use this method is need set UIviewController identifier into storyboard right panel.
Using this isn't need to instantiate new UIViewController if you have it in storyboard.
I solved it by thinking about loading sequence.
All what to do is create temporary #properties
#interface DetailViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIImageView *branchImage;
#property (strong, nonatomic) IBOutlet UILabel *titleLabel;
#property (strong, nonatomic) IBOutlet UITextView *textView;
#property (nonatomic) NSString *titleTMP; //added
#property (nonatomic) NSString *subtitleTMP; //added
and do
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(#"%#", view.annotation.title);
MapPoint *annView = view.annotation;
DetailViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailViewController"];
dvc.title = #"my own Details";
dvc.titleTMP = annView.title;
dvc.subtitleTMP = annView.subtitle;
[self.navigationController pushViewController:dvc animated:YES];
}
The DetailViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
titleLabel.text = titleTMP;
textView.text = subtitleTMP;
}

XCode 4.2: Semantic Issue '#end' is missing in implementation context & Unexpected '#' in program

I am new to XCODE, but I am trying to learn. Basically my project is a rework of Nada Jaksic's Creating a table-based form on the iPhone". I would appreciate being informed where I went wrong in my coding, so that I can learn from my mistakes. Any and all help is definitely appreciated!
From the SignupTableViewController.h file:
#import <UIKit/UIKit.h>
#interface SignupTableViewController : UITableViewController<UITextFieldDelegate, UIActionSheetDelegate>
{
IBOutlet UITableViewCell *cellExaminername;
IBOutlet UITableViewCell *cellExamineraddress;
IBOutlet UITableViewCell *cellExaminercity;
IBOutlet UITableViewCell *cellExaminerstate;
IBOutlet UITableViewCell *cellExaminerzip;
IBOutlet UITableViewCell *cellExamineremail;
IBOutlet UITableViewCell *cellSearchthroughdate;
IBOutlet UITableViewCell *cellTitleheldby;
IBOutlet UITableViewCell *cellPropertyaddress1;
IBOutlet UITableViewCell *cellPropertyaddress2;
IBOutlet UITableViewCell *cellPropertycity;
IBOutlet UITableViewCell *cellPropertystate;
IBOutlet UITableViewCell *cellProperyzip;
IBOutlet UITableViewCell *cellTitlesearchnotes;
IBOutlet UITextField* txtExaminername;
IBOutlet UITextField* txtExamineraddress;
IBOutlet UITextField* txtExaminercity;
IBOutlet UITextField* txtExaminerstate;
IBOutlet UITextField* txtExaminerzip;
IBOutlet UITextField* txtExamineremail;
IBOutlet UITextField* txtSearchthroughdate;
IBOutlet UITextField* txtTitleheldby;
IBOutlet UITextField* txtPropertyaddress1;
IBOutlet UITextField* txtPropertyaddress2;
IBOutlet UITextField* txtPropertycity;
IBOutlet UITextField* txtPropertystate;
IBOutlet UITextField* txtPropertyzip;
IBOutlet UITextField* txtTitlesearchnotes;
UIActivityIndicatorView* activityIndicator;
}
#property (nonatomic, retain) IBOutlet UITableViewCell *cellExaminername;
#property (nonatomic, retain) IBOutlet UITableViewCell *cellExamineraddress;
#property (nonatomic, retain) IBOutlet UITableViewCell *cellExaminercity;
#property (nonatomic, retain) IBOutlet UITableViewCell *cellExaminerstate;
#property (nonatomic, retain) IBOutlet UITableViewCell *cellExaminerzip;
#property (nonatomic, retain) IBOutlet UITableViewCell *cellExamineremail;
#property (nonatomic, retain) IBOutlet UITableViewCell *cellSearchthroughdate;
#property (nonatomic, retain) IBOutlet UITableViewCell *cellTitleheldby;
#property (nonatomic, retain) IBOutlet UITableViewCell *cellPropertyaddress1;
#property (nonatomic, retain) IBOutlet UITableViewCell *cellPropertyaddress2;
#property (nonatomic, retain) IBOutlet UITableViewCell *cellPropertycity;
#property (nonatomic, retain) IBOutlet UITableViewCell *cellPropertystate;
#property (nonatomic, retain) IBOutlet UITableViewCell *cellProperyzip;
#property (nonatomic, retain) IBOutlet UITableViewCell *cellTitlesearchnotes;
#property (nonatomic, retain) IBOutlet UITextField* txtExaminername;
#property (nonatomic, retain) IBOutlet UITextField* txtExamineraddress;
#property (nonatomic, retain) IBOutlet UITextField* txtExaminercity;
#property (nonatomic, retain) IBOutlet UITextField* txtExaminerstate;
#property (nonatomic, retain) IBOutlet UITextField* txtExaminerzip;
#property (nonatomic, retain) IBOutlet UITextField* txtExamineremail;
#property (nonatomic, retain) IBOutlet UITextField* txtSearchthroughdate;
#property (nonatomic, retain) IBOutlet UITextField* txtTitleheldby;
#property (nonatomic, retain) IBOutlet UITextField* txtPropertyaddress1;
#property (nonatomic, retain) IBOutlet UITextField* txtPropertyaddress2;
#property (nonatomic, retain) IBOutlet UITextField* txtPropertycity;
#property (nonatomic, retain) IBOutlet UITextField* txtPropertystate;
#property (nonatomic, retain) IBOutlet UITextField* txtPropertyzip;
#property (nonatomic, retain) IBOutlet UITextField* txtTitlesearchnotes;
-(void)signup;
#end
From my SignupTableViewController.m file:
#import "SignupTableViewController.h"
#implementation SignupTableViewController
#synthesize cellExaminername, cellExamineraddress, cellExaminercity, cellExaminerstate, cellExaminerzip, cellExamineremail, cellSearchthroughdate, cellTitleheldby, cellPropertyaddress1, cellPropertyaddress2, cellPropertycity, cellPropertystate, cellProperyzip, cellTitlesearchnotes;
#synthesize txtExaminername, txtExamineraddress, txtExaminercity, txtExaminerstate, txtExaminerzip, txtExamineremail, txtSearchthroughdate, txtTitleheldby, txtPropertyaddress1, txtPropertyaddress2, txtPropertycity, txtPropertystate, txtPropertyzip, txtTitlesearchnotes;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
//self.clearsSelectionOnViewWillAppear = NO;
UIBarButtonItem* btnSubmitSignup = [[UIBarButtonItem alloc]initWithTitle:NSLocalizedString(#"Join",#"") style:UIBarButtonItemStylePlain target:self action:#selector(signup)];
[[self navigationItem] setRightBarButtonItem:btnSubmitSignup];
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 14;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
return cellExaminername;
}
if (indexPath.row == 1) {
return cellExamineraddress;
}
if (indexPath.row == 2) {
return cellExaminercity;
}
if (indexPath.row == 3) {
return cellExaminerstate;
}
if (indexPath.row == 4) {
return cellExaminerzip;
}
if (indexPath.row == 5) {
return cellExamineremail;
}
if (indexPath.row == 6) {
return cellSearchthroughdate;
}
if (indexPath.row == 7) {
return cellTitleheldby;
}
if (indexPath.row == 8) {
return cellPropertyaddress1;
}
if (indexPath.row == 9) {
return cellPropertyaddress2;
}
if (indexPath.row == 10) {
return cellPropertycity;
}
if (indexPath.row == 11) {
return cellPropertystate;
}
if (indexPath.row == 12) {
return cellProperyzip;
}
if (indexPath.row == 13) {
return cellTitlesearchnotes;
}
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
#pragma mark -
#pragma mark Memory management
-(void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}
-(void) signup
{
// make sure the keyboard is hidden
[txtExaminername resignFirstResponder];
[txtExamineraddress resignFirstResponder];
[txtExaminercity resignFirstResponder];
[txtExaminerstate resignFirstResponder];
[txtExaminerzip resignFirstResponder];
[txtExamineremail resignFirstResponder];
[txtSearchthroughdate resignFirstResponder];
[txtTitleheldby resignFirstResponder];
[txtPropertyaddress1 resignFirstResponder];
[txtPropertyaddress2 resignFirstResponder];
[txtPropertycity resignFirstResponder];
[txtPropertystate resignFirstResponder];
[txtPropertyzip resignFirstResponder];
[txtTitlesearchnotes resignFirstResponder];
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[activityIndicator setCenter:CGPointMake(self.view.frame.size.width/2.0, self.view.frame.size.height/2.0+20)];
[activityIndicator startAnimating];
[self.view addSubview:activityIndicator];
if (![self validateSignupForm])
return;
//do your signup code here
}
-(BOOL)validateSignupForm
{
BOOL rtn = YES;
// Declare your Alert, NSArray, increment int
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:NSLocalizedString(#"FormIncomplete", #"") delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
NSArray *fieldArray;
int i = 0;
// Load up our field array with the UITextField Values
fieldArray = [NSArray arrayWithObjects:
[NSString stringWithFormat:#"%#",txtExaminername.text],
[NSString stringWithFormat:#"%#",txtExamineraddress.text],
[NSString stringWithFormat:#"%#",txtExaminercity.text],
[NSString stringWithFormat:#"%#",txtExaminerstate.text],
[NSString stringWithFormat:#"%#",txtExaminerzip.text],
[NSString stringWithFormat:#"%#",txtExamineremail],
[NSString stringWithFormat:#"%#",txtSearchthroughdate.text],
[NSString stringWithFormat:#"%#",txtTitleheldby.text],
[NSString stringWithFormat:#"%#",txtPropertyaddress1.text],
[NSString stringWithFormat:#"%#",txtPropertyaddress2.text],
[NSString stringWithFormat:#"%#",txtPropertycity.text],
[NSString stringWithFormat:#"%#",txtPropertystate.text],
[NSString stringWithFormat:#"%#",txtPropertyzip.text],
[NSString stringWithFormat:#"%#",txtTitlesearchnotes.text]];
// loop through the array, alert if text field is empty, and break the the loop, other wise increment i
for (NSString *fieldText in fieldArray){
NSLog(fieldText); // make sure all is reading correctly in the console
if([fieldText isEqualToString:#""]){
[alert show];
rtn = NO;
break; // break out!!
}
i++;
}
// check that all the field were passed (i == array.count)
//if(i == [[NSNumber numberWithInt: fieldArray.count] intValue]){
//NSLog(#"Passed validation...");
//rtn = YES;
// {
//if (rtn)
#end
Your -validateSignupForm method is missing a closing }.
A hint that this might be true is Xcode's desire to indent #end.

Converting UITextField to UILabel through view Controllers

I'm sure there is a simple explanation for this.
I am looking to transfer the data (not store) but transfer the text the user types into a text field and display it as a UILabel in another ViewController.
I already know how to convert text entered by the user into a label on the same viewcontroller.
I guess my problem is importing.
the .h:
#interface ViewController : UIViewController {
IBOutlet UITextField *firstPerson;
IBOutlet UITextField *secondPerson;
IBOutlet UIButton *calculateButton;
NSString *firstName;
NSString *secondName;
}
#property (nonatomic, retain) IBOutlet UITextField *firstPerson;
#property (nonatomic, retain) IBOutlet UITextField *secondPerson;
#property (nonatomic, retain) NSString *firstName;
#property (nonatomic, retain) NSString *secondName;
#property (nonatomic, retain) IBOutlet UIButton *calculateButton;
-(IBAction)calculate;
#end
the .m:
-(IBAction)calculate {
//Linked to UIButton
//This is the first View Controller.
// firstName = firstPerson.text;
// secondName = secondPerson.text;
secondViewController = [[ShowStats alloc] init];
}
my secondview controller .m (ShowStats):
#import "ShowStats.h"
#import "ViewController.h"
- (void)viewDidLoad
{
firstName = firstPerson.text;
secondName = secondPerson.text;
[super viewDidLoad];
}
Many thanks!
EDIT
ViewController.h
#import <UIKit/UIKit.h>
#import "ShowStats.h"
#interface ViewController : UIViewController {
IBOutlet UITextField *firstPerson;
IBOutlet UITextField *secondPerson;
IBOutlet UIButton *calculateButton;
//NSString *firstName;
// NSString *secondName;
}
#property (nonatomic, retain) IBOutlet UITextField *firstPerson;
#property (nonatomic, retain) IBOutlet UITextField *secondPerson;
//#property (nonatomic, retain) NSString *firstName;
//#property (nonatomic, retain) NSString *secondName;
#property (nonatomic, retain) IBOutlet UIButton *calculateButton;
-(IBAction)calculate;
#end
ViewController.m
#import "ViewController.h"
#import "ShowStats.h"
#implementation ViewController
#synthesize firstPerson, secondPerson;
//#synthesize firstName, secondName;
#synthesize calculateButton;
ShowStats *secondViewController;
-(IBAction)calculate {
secondViewController = [[ShowStats alloc] init];
secondViewController.firstName = firstPerson.text;
}
ShowStats.h
#interface ShowStats : UIViewController{
IBOutlet UILabel *nameStats;
}
#property (nonatomic, retain) IBOutlet UILabel *nameStats;
#property (nonatomic, retain) NSString *firstName;
#property (nonatomic, retain) NSString *secondName;
#end
ShowStats.m
- (void)viewDidLoad
{
nameStats.text = [NSString stringWithFormat:#"%#", firstName];
//secondLabel.text = self.secondName;
[super viewDidLoad];
}
Make these properties in ShowStats class
#property (nonatomic, retain) NSString *firstName;
#property (nonatomic, retain) NSString *secondName;
and change this to this
-(IBAction)calculate {
secondViewController = [[ShowStats alloc] init];
secondViewController.firstName = firstPerson.text;
secondViewController.secondName = secondPerson.text;
}
then set these strings to UILablel in your viewDidLoad
In ShowStats.h, add the following:
#property(nonatomic, copy) NSString* firstName;
#property(nonatomic, copy) NSString* secondName;
In ShowStats.m add/update the following:
#synthesize firstName, secondName;
//...
- (id) init {
if (self = [super init]) {
self.firstName = nil;
self.secondName = nil;
//...
}
return self;
}
- (void) dealloc {
self.firstName = nil;
self.secondName = nil;
//...
[super dealloc];
}
- (void)viewDidLoad {
//or even better, do this in viewWillAppear instead
firstLabel.text = self.firstName;
secondLabel.text = self.secondName;
//...
[super viewDidLoad];
}
Finally, in ViewController.m, implement calculate like this:
-(IBAction)calculate {
//Linked to UIButton
//This is the first View Controller.
// firstName = firstPerson.text;
// secondName = secondPerson.text;
secondViewController = [[ShowStats alloc] init];
secondViewController.firstName = firstPerson.text;
secondViewController.secondName = secondPerson.text;
//display the view controller here
[secondViewController autorelease];
}
If it is a navigation then in your SecondViewController you can call:
ViewController *viewController = [self.navigationController.viewControllers objectAtIndex:0];
firstName = [[viewController firstPerson] text];
secondName = [[viewController secondPerson] text];
Or you could do the following for a Single View app:
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
ViewController *viewController = [appDelegate viewController];
firstName = [[viewController firstPerson] text];
secondName = [[viewController secondPerson] text];
* EDIT *
Add two labels to your .h file (nonatomic, retain) & synthesize in .m . Initialize the labels in viewDidLoad then set them (assuming they are called label1 and label2):
[label1 setText:firstName];
[label2 setText:secondName];

objectForKey returns nil on a dictionary

I am creating a recipe manager app for iPad. I am using the split view template which loads data for the recipes from a plist which is structured in this way. The table view on the left works perfectly. It loads the data from the plist. The problem is the detail view. As you can see I have inserted a NSLog lines in the DetailViewController.m file since every string I passed to the text field resulted as nil. Strangely, when I tried to NSLog from the RootViewController.m with:
NSLog(#"%#", [detailViewController.ricetta allKeys])
it worked.
Any suggestions?
I tried to post as much lines as possible to explain the problem as well as possible.
<plist version="1.0">
<array>
<dict>
<key>name</key>
<string>Risotto alla zucca</string>
<key>ingredients</key>
<string></string>
<key>description</key>
<string></string>
<key>photo</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Semi di zucca al forno</string>
<key>ingredients</key>
<string></string>
<key>description</key>
<string></string>
<key>photo</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Risotto ai funghi</string>
<key>ingredients</key>
<string></string>
<key>description</key>
<string></string>
<key>photo</key>
<string></string>
</dict>
</array>
</plist>
RootViewController.h
#import <UIKit/UIKit.h>
#import "RecipeConstants.h"
#class DetailViewController;
#interface RootViewController : UITableViewController {
NSMutableArray *ricette;
}
#property (nonatomic, retain) NSMutableArray *ricette;
#property (nonatomic, retain) IBOutlet DetailViewController *detailViewController;
#end
RootViewController.m
...
#synthesize ricette;
- (void)viewDidLoad
{
[super viewDidLoad];
self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
NSString *path = [[NSBundle mainBundle] pathForResource:#"RicetteArray" ofType:#"plist"];
NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
self.ricette = tmpArray;
[tmpArray release];
}
...
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.ricette count];
}
// Configure the cell.
cell.textLabel.text = [[self.ricette objectAtIndex:indexPath.row] objectForKey:NAME_KEY];
return cell;
}
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
detailViewController.ricetta = [self.ricette objectAtIndex:indexPath.row];
}
...
- (void)dealloc
{
[detailViewController release];
[ricette release];
[super dealloc];
}
#end
DetailViewController.h
#import <UIKit/UIKit.h>
#import "RecipeConstants.h"
#interface DetailViewController : UIViewController <UIPopoverControllerDelegate, UISplitViewControllerDelegate> {
IBOutlet UILabel *lblTitolo;
IBOutlet UITextView *ingredientiTxtView;
IBOutlet UITextView *descrizioneTxtView;
IBOutlet UIImageView *fotoImgView;
NSDictionary *ricetta;
}
#property (nonatomic, retain) IBOutlet UILabel *lblTitolo;
#property (nonatomic, retain) IBOutlet UITextView *ingredientiTxtView;
#property (nonatomic, retain) IBOutlet UITextView *descrizioneTxtView;
#property (nonatomic, retain) IBOutlet UIImageView *fotoImgView;
#property (nonatomic, retain) NSDictionary *ricetta;
#property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
#property (nonatomic, retain) id detailItem;
#end
DetailViewController.m
#synthesize lblTitolo, ingredientiTxtView, descrizioneTxtView, fotoImgView, ricetta;
...
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
lblTitolo.text = [ricetta objectForKey:NAME_KEY];
ingredientiTxtView.text = [ricetta objectForKey:INGREDIENTS_KEY];
descrizioneTxtView.text = [ricetta objectForKey:DESCRIPTION_KEY];
UIImage *img = [UIImage imageNamed:PHOTO_KEY];
fotoImgView.image = img;
NSLog(#"%#", [ricetta allKeys]);
}
...
- (void)dealloc
{
[_myPopoverController release];
[_toolbar release];
[_detailItem release];
[lblTitolo release];
[ingredientiTxtView release];
[descrizioneTxtView release];
[fotoImgView release];
[ricetta release];
[super dealloc];
}
#end
UPDATE:
Actually in the DetailViewController.m I have this method:
- (void)updateRicetta {
lblTitolo.text = [ricetta objectForKey:NAME_KEY];
NSString *myIngredients = [ricetta objectForKey:INGREDIENTS_KEY];
myIngredients = [myIngredients stringByReplacingOccurrencesOfString:#", " withString:#"\n"];
ingredientiTxtView.text = myIngredients;
NSString *myDescription = [ricetta objectForKey:DESCRIPTION_KEY];
myDescription = [myDescription stringByReplacingOccurrencesOfString:#". " withString:#"\n"];
descrizioneTxtView.text = myDescription;
NSString *nomeImmagine = [NSString stringWithFormat:#"%#.jpg", [ricetta objectForKey:PHOTO_KEY]];
[self.fotoImgView setImage:[UIImage imageNamed:nomeImmagine]];
}
which is called from RootViewController.m in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
using
[detailViewController updateRicetta];
I have the following two warnings:
'DetailViewController' may not respond to '-updateRicetta'
Instance method '-updateRicetta not found (return type defaults to 'id')
Another problem is that the detail view updates only when I click on a cell not at app loading.
I'm happy to be wrong about this, but it looks to me like when you call tableView:didSelectRowAtIndexPath: you set the recipe (ricetta?) in the DetailViewController but I don't see any instruction to tell the DetailViewController to display the new recipe.
You have code to update your DetailViewController in viewWillAppear:animated: but this will only happen when the view is going to appear - if your view has already appeared this method won't be executed.
I suggest you create a method in DetailViewController called updateRicetta which will look something like:
- (void)updateRicetta {
lblTitolo.text = [ricetta objectForKey:NAME_KEY];
ingredientiTxtView.text = [ricetta objectForKey:INGREDIENTS_KEY];
descrizioneTxtView.text = [ricetta objectForKey:DESCRIPTION_KEY];
UIImage *img = [UIImage imageNamed:PHOTO_KEY];
fotoImgView.image = img;
}
You could then call this from tableView:didSelectRowAtIndexPath: -
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
detailViewController.ricetta = [self.ricette objectAtIndex:indexPath.row];
[detailViewController updateRicetta];
}
As to the returning nil: my guess is that when viewWillAppear:animated: is called you have not yet selected an entry and therefore ricetta has not yet been set in the DetailViewController.