Showing posts with label ios. Show all posts
Showing posts with label ios. Show all posts

Monday, 5 August 2013

Difference between #import and #include in Objective C code ? iPhone/ XCode programming

#import and #include are like same. They perform the similar thing but the only difference is that #import makes sure that the same header file hasn't included again.

It is preferred to write #import over #include in your code. 

- Reference Learning from iOS Developer Library

Monday, 22 April 2013

How to set default text or hint in UITextField ?

In textfields, sometimes we need to show some default message which is a hint which disappears when gets focused or is in editing mode. So, for this do you the placeholder property of textfield.







For this just write the text you want to show in the field placeholder in property window.



Password and Confirm Password in UITextField Xcode

You can write the code below on your button touch event of either sign up or login. This will check whether the strings/passwords written in both UITextFields are same or not and will prompt an alert message :

iif ([self.pwd.text isEqualToString:self.confirmpwd.text])
    {
        UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"Message" message:@"successfully" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel", nil];
        
        [alert show];
    }
    
    else
    {
        UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"Message" message:@"Password mismatch" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        
        [alert show];
        
    }

Password validation in UITextField

Code for password validation is as below, written in textFieldDidEndEditing method of UITextField delegate :

*viewcontroller.h
Example :

@interface SignUpViewController : UIViewController<UITextFieldDelegate>
@property (nonatomic,retain)IBOutlet UITextField *pwd;

*viewcontroller.m

@synthesize pwd;

- (void)textFieldDidEndEditing:(UITextField *)textField
{


   if (textField == pwd)
    {
        int numberofCharacters = 0;
        BOOL lowerCaseLetter,upperCaseLetter= 0;
        if([pwd.text length] >= 10)
        {
            for (int i = 0; i < [pwd.text length]; i++)
            {
                unichar c = [pwd.text characterAtIndex:i];
                if(!lowerCaseLetter)
                {
                    lowerCaseLetter = [[NSCharacterSet lowercaseLetterCharacterSet] characterIsMember:c];
                }
                if(!upperCaseLetter)
                {
                    upperCaseLetter = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:c];
                }
                /*if(!digit)
                {
                    digit = [[NSCharacterSet decimalDigitCharacterSet] characterIsMember:c];
                }
                if(!specialCharacter)
                {
                    specialCharacter = [[NSCharacterSet symbolCharacterSet] characterIsMember:c];
                }*/
            }
            
            if(lowerCaseLetter && upperCaseLetter)
            {
                //do what u want
            }
            else
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                message:@"Please Ensure that you have at least one lower case letter, one upper case letter, one digit and one special character"
                                                               delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];
            }
            
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                            message:@"Please Enter at least 10 password"
                                                           delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }

    }
}

Email validation in UITextField

Create a function for validating email like the code below :


- (BOOL)validateEmail:(NSString *)emailStr 
{

  NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
   NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:emailStr];

}

UITextField delegate has a method textFieldDidEndEditing, write the code below to display an alert message when email is not valid :

if (textField == email)
    {
        
if(![self validateEmail:[email text]])
        {
            // user entered invalid email address
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Enter a valid email address." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
            [alert show];
           
            //email.text=@"";
        }
    }

How to use UINavigationViewController in Xcode ?

Hello World, 

I thought that navigation is one of the useful thing we do, so thought for a little help making a tutorial. So, here's how we do it :

If you are using storyboard then there's no need of this but if you are going through xib then learn using this method.

So, first of all do include the below code in your appdelegate.m file otherwise your navigation would never work :




Now, on a button touch event, we perform the navigation to the another screen. So, I've created an object of secondview here.

Watch the video to understand it well :)

Saturday, 13 April 2013

Cannot use super because it is a root class error in Xcode project !



Getting an error "cannot use super because it is a root class" and wasn't able to find the proper solution. After a while found that have misplaced the name so "self" calling was not working.

This error may appear either you haven't written @interface "" @end or misplaced the name of it or written a different name after "@implementation"

e.g. in above image, as I was working on CoRoViewThree_iPad, when I replaced "Four" with "Three" and similarly Three instead of Two in @implementation, the error resolved.


Such kind of error mostly occurs when we copy the codes of different view controllers and forget to change the names.