p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: iPhone and iPad App 24-Hour Trainer (http://p2p.wrox.com/forumdisplay.php?f=686)
-   -   Chapter 6 Errors (http://p2p.wrox.com/showthread.php?t=90397)

Karl June 23rd, 2013 08:09 PM

Chapter 6 Errors
 
Hello there,

I am new to programming and I'm getting on well with your book. I do find that there are some things which appear in the videos and downloaded code which differ greatly from what appears in my X Code. I put it down to differing versions of your XCode and mine. Sometimes I implement what XCode says I should do to correct the errors and sometimes I don't. I've got away with it so far, but now I'm stuck.

My Lesson6ViewController.h looks the same as yours,

My Lesson6ViewController.m has 8 errors and there is a lot of extra code in both the video and the downloaded example. Can you help, I would really appreciate your advice.
Here's the offending code, below.
Thanks, Karl.

OSX 10.7.5, XCode 4.6.3 (4H1503)

--------------------------------------------------------------------------
Code:

Lesson6ViewController.m

    NSString* username = usernameField.text;    ---Error "Use of  undeclared identifier 'usernameField'---                                                   
   
    int length = [username length];                  ---Error "Initializer element is not a compile-time constant---
    if (length == 0)                              ---Error "Expected identifier or'(' "---
        return;
       
    NSString* alertMessage = [NSString stringWithFormat:@"Welcome %@",    ---Error "Initializer element is not a compile-time constant---
                              username];
   
        UIAlertView* welcomeMessage = [[UIAlertView alloc]    ---Error "Initializer element is not a compile-time constant----
                                      initWithTitle:@"Login Successful"
                                      message:alertMessage
                                      delegate:nil
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];

[welcomeMessage show];                              ---Error "Missing '[' at start of message send expression (3)"---

- (void) handleBackgroundTap:(UITapGestureRecognizer *)sender
{
    [usernameField resignFirstResponder];
}
                               
@end
 --------------------------------------------------------------------------
Lesson6ViewController.h

#import <UIKit/UIKit.h>

@interface Lesson6ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *usernameField;
@property (weak, nonatomic) IBOutlet UITextField *passwordField;
- (IBAction)onDismissKeyboard:(id)sender;
- (IBAction)onLogin:(id)sender;
- (void) handleBackgroundTap:(UITapGestureRecognizer*)sender;

@end

--------------------------------------------------------------------------

thepianoguy June 30th, 2013 01:23 PM

Can you put the entire .h and .m file for your ViewController in a reply? The error is most likely above what you have pasted from the .m file.

Bob

Karl July 10th, 2013 09:27 PM

Lesson 6 My Errors
 
Hello Bob, thanks for replying,

Here's my .h
Code:

#import <UIKit/UIKit.h>

@interface Lesson6ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *usernameField;
@property (weak, nonatomic) IBOutlet UITextField *passwordField;
- (IBAction)onDismissKeyboard:(id)sender;
- (IBAction)onLogin:(id)sender;
- (void) handleBackgroundTap:(UITapGestureRecognizer*)sender;

@end

And here's my .m
Code:

#import "Lesson6ViewController.h"

@interface Lesson6ViewController ()




@end
@implementation Lesson6ViewController
@synthesize usernameField;
@synthesize passwordField;

- (void)viewDidLoad
{
    [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc]
                                            initWithTarget:self
                                            action:@selector(handleBackgroundTap:)];
    tapRecognizer.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tapRecognizer];
   
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)onDismissKeyboard:(id)sender {
    [usernameField resignFirstResponder];
    [passwordField resignFirstResponder];

}

- (IBAction)onLogin:(id)sender {
    [usernameField resignFirstResponder];
    [passwordField resignFirstResponder];
}
   
    NSString* username = usernameField.text;
   
    int length = [username length];
    if (length == 0)
        return;
       
    NSString* alertMessage = [NSString stringWithFormat:@"Welcome %@",
                              username];
   
        UIAlertView* welcomeMessage = [[UIAlertView alloc]
                                      initWithTitle:@"Login Successful"
                                      message:alertMessage
                                      delegate:nil
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];

[[welcomeMessage show]];
 

- (void) handleBackgroundTap:(UITapGestureRecognizer *)sender
{
    [usernameField resignFirstResponder];
}

@end

Thanks for the help, I appreciate it.
Karl

thepianoguy July 11th, 2013 09:24 AM

There are a couple of corrections that should solve your problems.

Code:

- (IBAction)onLogin:(id)sender {
    [usernameField resignFirstResponder];
    [passwordField resignFirstResponder];
} This curly brace should not be here. There following lines are part of the same method, up to and including [welcomeMessage show];
   
    NSString* username = usernameField.text;
   
    int length = [username length];
    if (length == 0)
        return;
       
    NSString* alertMessage = [NSString stringWithFormat:@"Welcome %@",
                              username];
   
        UIAlertView* welcomeMessage = [[UIAlertView alloc]
                                      initWithTitle:@"Login Successful"
                                      message:alertMessage
                                      delegate:nil
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];

[[welcomeMessage show]]; There should only be one bracket on either side of the message [welcomeMessage show];, not the double that is there.
 This is the point where you should add your closing curly brace}

- (void) handleBackgroundTap:(UITapGestureRecognizer *)sender
{
    [usernameField resignFirstResponder];
}

Here is the correct version in case what I wrote is not clear

Code:

#import "Lesson6ViewController.h"

@interface Lesson6ViewController ()




@end
@implementation Lesson6ViewController
@synthesize usernameField;
@synthesize passwordField;

- (void)viewDidLoad
{
    [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc]
                                            initWithTarget:self
                                            action:@selector(handleBackgroundTap:)];
    tapRecognizer.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tapRecognizer];
   
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)onDismissKeyboard:(id)sender {
    [usernameField resignFirstResponder];
    [passwordField resignFirstResponder];
   
}

- (IBAction)onLogin:(id)sender {
    [usernameField resignFirstResponder];
    [passwordField resignFirstResponder];


NSString* username = usernameField.text;

int length = [username length];
if (length == 0)
return;

NSString* alertMessage = [NSString stringWithFormat:@"Welcome %@",
                          username];

UIAlertView* welcomeMessage = [[UIAlertView alloc]
                              initWithTitle:@"Login Successful"
                              message:alertMessage
                              delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];

[welcomeMessage show];

}
- (void) handleBackgroundTap:(UITapGestureRecognizer *)sender
{
    [usernameField resignFirstResponder];
}

@end

The misplaced curly brace } is the root cause of your error messages. You will notice symptoms of syntax errors when autocomplete starts suggesting unusual choices and indentation stops formatting correctly.
Having the double bracket around [[welcomeMessage show]] implies that the result of the first message (inside bracket pair) is receiving another message. There is no another message. You can see how this is used in the various [[SomeObject alloc] init] statements that appear in the program. e.g. the UIAlertView.

Making these corrections should solve your problems, assuming everything is properly connected in the Storyboard. If not, let me know.

The additional code that appears in the author's download version and the book are stub methods, some of which are obsolete in iOS 6, and others that are rarely implemented. The use of ARC has made most memory management automatic. For example -viewDidUnload is never called in iOS 6, and weak pointers (IBOutlets that are weak pointers for example) are automatically set to nil when the view that owns them is deallocated. Under earlier versions, you were responsible for releasing the memory and making everything safe by setting to nil. Useful discussions can be found on StackExchange, and helpful tutorials on Ray Wenderlich's site.



Good luck.


Bob

Karl July 14th, 2013 05:16 AM

Thanks Bob
 
Thanks Bob, that got rid of all the errors, my code is the same as yours. However the keyboard does not dismiss. When the Login button is pressed it highlights, but the alert view message does not appear.
I have compared the outlets from the text fields and the button in the connections inspector and everything seems correct. It probably isn't.

I also have a problem with a 3 component UIPicker project. The picker performs correctly, but I cannot get the the the rows to output to a label in anyway. I have tried many different ways and code from the internet, but I suspect it is the connections doing the damage.
I wish I could use the debugger.

Any thoughts?
Karl

thepianoguy July 14th, 2013 08:53 AM

If the problem is in the outlet connections, I would be able to troubleshoot more effectively if I have the actual project. Feel free to send the 2 projects to ipadhelper@me.com
or place them on a file share where I can have access to them and I will take a look.

Bob

Karl July 22nd, 2013 11:41 PM

Fixed them Thanks
 
Hello Bob,
Thanks for the info. I redid Lesson 6 from scratch, keeping careful eye on what you said and it works well. I also managed to clear up my other project by modifying the UIAlertview output from Lesson 9 to go to my label.
I just have to integrate Facebook and Twitter next.
Once again, thanks for your help.

Karl


All times are GMT -4. The time now is 10:33 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.