Thursday, January 10, 2013

Creating UIAlertView and UIActionSheet Programmatically

For Beginner's, you may get a doubts like  this
1.what is the difference between UIAlertView and UIActionSheet?
2. In which scenarios we will use these controls?
Now i am going to clarify your doubts

UIAlertView

  •  Alerts give users important information that affects their use of the application (or the device). Alerts are usually unexpected, because they generally tell users about a problem or a change in the current situation that might require users to take action.
EXAMPLE:
      Giving alert when your Mobile Battery is low,
      we can  also design Login form using alert View


Creating AlertView programmatically
UIAlertView *alert = [[UIAlertView alloc]
   initWithTitle: @"AlertView"
   message: @"Welcome To IOS Controls!"
   delegate: nil
   cancelButtonTitle:@"OK"
   otherButtonTitles:nil];
[alert show];

The above code  pop an alert with a title, message and OK 


button like this


NOTE:
If you want  to know  the properties and methods that are present in the Class Or Control, put the cursor on the Class and command+click.You will get that class properties and Methods.

But  if you want to have more than one button on the alert 

and collect user input from it? . To do that, you first need to 

create a class that employs the UIAlertViewDelegate delegate 

like this:

@interface PresentAlert : NSObject <UIAlertViewDelegate>

Then, in PresentAlert.m file, viewDidLoad Method create 

alertView  like this
UIAlertView *alertView = [[UIAlertView alloc]
   initWithTitle: @"AlertView"
   message: @"Welcome To IOS Controls!"
   delegate: self
   cancelButtonTitle:@"Cancel"
   otherButtonTitles:@"Submit",nil];
[alert show];

 you need to override the  alertView  method in order to 

receive input like this:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
 if (buttonIndex == 0) {
  NSLog(@"Cancel Pressed");
 }
 else  if (buttonIndex==1){
  NSLog(@"Submit Pressed");
 }
}

 Different styles of alertView's are

a.alertView.alertViewStyle = UIAlertViewStyleSecureTextInput.

It pop an alert with textfield,the entered text will be in secure format

b. alertView.alertViewStyle = UIAlertViewStylePlainTextInput.

It pop an alert with textfield,the entered text will be plain format.

c.alertView.alertViewStyle= UIAlertViewStyleLoginAndPasswordInput.

It pop an alert with two textfield's one for user name and another one for pasword.

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

UIActionSheet

  • Action sheets give users additional choices related to the action they are currently taking. 

EXAMPLE:
   If you want to delete the recent call list from your mobile,
action sheet will give options like
           -->Delete Dialed Calls.
           -->Delete Received Calls.
           -->Delete Missed Calls.
           -->Cancel 
Creating UIActionSheet Programmatically











Wednesday, January 9, 2013

Handling Touch Events For UIButton in IOS



I recently used this code for handling touch events like touchesBegan,touchesMoved,touchesEnded  on UIButton in my project.

STEP 1:Create  UIButton Programmatically like this

1.UIButton *touchButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
2.    touchButton.frame=CGRectMake(303026050);
    [touchButton setTitle:@"TOUCH BUTTON" forState:UIControlStateNormal];
 3.   [touchButton addTarget:self action:@selector(touchBegan:withEvent:) forControlEventsUIControlEventTouchDown];
 4.   [touchButton addTarget:self action:@selector(touchMoving:withEvent:) forControlEventsUIControlEventTouchDragInside | UIControlEventTouchDragOutside];
  5.  [touchButton addTarget:self action:@selector(touchEnded:withEvent:) forControlEventsUIControlEventTouchUpInside | UIControlEventTouchUpOutside];
6. [self.view addSubview:touchButton];

In the above code  adding selectors for different control events is important.
if you want to recognize touchBegan Method on UIButton,you have to write the selector method like this 

[touchButton addTarget:self action:@selector(touchBegan:withEvent:) forControlEventsUIControlEventTouchDown];
if you want to recognize touchMoving Method on UIButton,you have to write the selector method like this 

[touchButton addTarget:self action:@selector(touchMoving:withEvent:) forControlEventsUIControlEventTouchDragInside | UIControlEventTouchDragOutside];
if you want to recognize touchEnded Method on UIButton,you have to write the selector method like this 

[touchButton addTarget:self action:@selector(touchEnded:withEvent:) forControlEventsUIControlEventTouchUpInside | UIControlEventTouchUpOutside];

STEP 2: Implement The Selector Methods For Touch Events Like this.
// this method calls whenever you touchDown on button
- (void)touchBegan:(UIButton *)control withEvent:event {
    NSLog(@"touchBegan called......");
    UITouch *touch = [[event allTouchesanyObject];
    CGPoint touchPoint = [touch locationInView:self.view];
    NSLog(@"In Touch Began: Touch x : %f y : %f", touchPoint.x, touchPoint.y);
    
}
// this method calls whenever you touchDragInside or touchDragOutside on button
- (void)touchMoving:(UIButton *)control withEvent:event {
    NSLog(@"touchMoving called......");
    UITouch *touch = [[event allTouchesanyObject];
    CGPoint touchPoint = [touch locationInView:self.view];
     NSLog(@"In Touch Moving  :Touch x : %f y : %f", touchPoint.x, touchPoint.y);
   }

// this method calls whenever you touchUp inside or touchUp outside on button
- (void)touchEnded:(UIButton *)addOnButton withEvent:event {
    NSLog(@"touchEnded called......");
    UITouch *touch = [[event allTouchesanyObject];
    CGPoint touchPoint = [touch locationInView:self.view];
    NSLog(@" In Touch Ended : touchpoint.x and y is %f,%f",touchPoint.x,touchPoint.y);
}


Thank You.....