May 16 2012

Use UITableView

Mohammed Al-Atari
Sample code here
  1. Create new Xcode project
  2. Add UITableView to ViewController.xib
  3. RightClick on UiTableView and link Datasource  to file owners
  4. code for ViewController.h as below
    #import <UIKit/UIKit.h>@interface ViewController : UIViewController<UITableViewDataSource>

    {

    NSArray *names;

    }

    @end

  5. code for ViewController.m as below
    #import “ViewController.h”@implementation ViewController

    -(void)dealloc{

    [names release];

    [super dealloc];

    }
    -(void)viewDidLoad{

    [super viewDidLoad];

    //Load Array with init. data to be shown in table view

    names = [[NSArray alloc]

    initWithObjects:@”Mohammed”,@”Hasan”,@”Ahmad”,@”Abdallah”, nil];

    //Below code for load the array data from static file

    //NSString *filepath =  [[NSBundle mainBundle]

    //                       pathForResource:@”data”

    //                       ofType:@”plist”];

    //

    //names =  [[NSArray alloc] initWithContentsOfFile:filepath];

    }

     

     

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [names count];

    }

     

     

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    //Ask the table for a reusable cells

    UITableViewCell *cell =

    [tableView dequeueReusableCellWithIdentifier:@"cell"];

    //If no reusable cells exist, then create a new one

    if (cell == nil) {

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@”cell”] autorelease];

    }

    //Configure the cell

    cell.textLabel.text = [names objectAtIndex:indexPath.row];

    return cell;

    }

     

     

     

    @end


May 13 2012

How to Dismiss the Keyboard when using a UITextView

Mohammed Al-Atari

Oddly this was more tricky then I would have thought … perhaps for a veteran Cocoa developer this would have been obvious, but for the rest of us struggling to get rid of the keyboard on a UITextView here is the secret sauce …

The short answer is to send the UITextViewController the “resignFirstResponder” message … the trick however is when to send the message. In my case, and I assume it would be the same for others, is to listen for any changes to the text in the UITextView and if the carridge return character ‘\n’ is detected then send the “resignFirstResponder” message to the UITextView.

Step 1. The first step is to make sure that you declare support for the UITextViewDelegate protocol. This is done in your header file, as example here is the header called EditorController.h:

1
2
3
4
5
6
7
@interface EditorController : UIViewController  {
  UITextView *messageTextView;
}

@property (nonatomic, retain) UITextView *messageTextView;

@end

Step 2. Next you will need to register the controller as the UITextView’s delegate. Continuing from the example above, here is how I have initialize the UITextView with EditorController as the delegate …

1
2
3
4
5
6
7
8
9
10
11
- (id) init {
    if (self = [super init]) {
        // define the area and location for the UITextView
        CGRect tfFrame = CGRectMake(10, 10, 300, 100);
        messageTextView = [[UITextView alloc] initWithFrame:tfFrame];
        // make sure that it is editable
        messageTextView.editable = YES;

        // add the controller as the delegate
        messageTextView.delegate = self;
    }

Step 3. And now the final piece of the puzzle is to take action in response to the “shouldCahngeTextInRange” message as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
  replacementText:(NSString *)text
{
    // Any new character added is passed in as the "text" parameter
    if ([text isEqualToString:@"\n"]) {
        // Be sure to test for equality using the "isEqualToString" message
        [textView resignFirstResponder];

        // Return FALSE so that the final '\n' character doesn't get added
        return FALSE;
    }
    // For any other character return TRUE so that the text gets added to the view
    return TRUE;
}



May 4 2012

Important XCode Tips

Mohammed Al-Atari

 

 

  • Declare a string variable and release it from memory manually

 NSString *str1 = [[NSString alloc] initWithString:@”Hello World …”];

 NSLog(@”%@”,str1);

 [str1 release];

 

  • Declare a string variable and automatically release it from memory

 NSString *str1 = [NSString stringWithString:@"Hello World ..."];

 NSLog(@”%@”,str1);

 OR

 NSString *str1 = @”Hello World …”;

 NSLog(@”%@”,str1);

 

  • Append string variable with new string

 NSString *str1 = @”Hello World …”;

 str1 =  [str1 stringByAppendingString:@", Goodbye world"];

 NSLog(@”%@”,str1);

 

  • Grab a substring of a given string from start till character index

 NSString *str1=@”Hollo World”;

 str1=[str1 substringToIndex:3];

 NSLog(@”%@”, str1);

 Output:

 2012-05-03 20:16:42.245 Project_7[74969:903] Hol

 

  • Grab a substring of a given string from character index till end of string

 NSString *str1=@”Hollo World”;

 str1=[str1 substringFromIndex:3];

 NSLog(@”%@”, str1);

 Output:

 2012-05-03 20:16:42.245 Project_7[74969:903] lo World

 

  • Use Dynamic array with memory allocation

 NSMutableArray *DynArray = [[NSMutableArray alloc] initWithObjects:@”Hello World”, nil];

 [DynArray addObject:@"Goodbye World"];

 NSLog(@”%@”,DynArray);

 [DynArray release];

 Output:

 2012-05-03 19:57:21.555 Project_7[74850:903] (

    “Hello World”,

    “Goodby World”

 )

 

  • Change array index order

 NSMutableArray *DynArray = [[NSMutableArray alloc] initWithObjects:@”Hello   World”,@”Goodbye World”, nil];

 [DynArray exchangeObjectAtIndex:0 withObjectAtIndex:1];

 NSLog(@”%@”,DynArray);

 [DynArray release];

 Output:

 2012-05-03 20:01:58.293 Project_7[74896:903] (

    “Goodby World”,

    “Hello World”

  )

 

  • Remove object index from array

 NSMutableArray *DynArray = [[NSMutableArray alloc] initWithObjects:@”Hello World”,@”Goodbye World”, nil];

 [DynArray removeObject:@"Goodby World"];

 NSLog(@”%@”,DynArray);

 [DynArray release];

 Output:

 2012-05-03 20:05:52.639 Project_7[74933:903] (

    “Hello World”

 )

 

  • Use NSDictionary for store one table name

 NSDictionary *DicObj = [NSDictionary dictionaryWithObjectsAndKeys:

                            @"Mohammed", @"FirstName",

                            @"Al-Atari", @"LastName",

                            @"23", @"Age", nil];

 NSLog(@”%@” , DicObj);

 NSLog(@”%@”, [DicObj objectForKey:@"FirstName"]);

 Output:

 2012-05-03 20:36:41.401 Project_7[75304:903] {

    Age = 23;

    FirstName = Mohammed;

    LastName = “Al-Atari”;

 }

 2012-05-03 20:37:40.666 Project_7[75332:903] Mohammed

 

  •  Use NSMutableDictionary for store one table name with initializing the object

 NSMutableDictionary *DicObj = [NSMutableDictionary dictionaryWithObjectsAndKeys:

                            @"Mohammed", @"FirstName",

                            @"Al-Atari", @"LastName",

                            @"23", @"Age", nil];

 NSLog(@”%@” , DicObj);

 NSLog(@”%@”, [DicObj objectForKey:@"FirstName"]);

 Output:

 2012-05-03 20:36:41.401 Project_7[75304:903] {

    Age = 23;

    FirstName = Mohammed;

    LastName = “Al-Atari”;

 }

 2012-05-03 20:37:40.666 Project_7[75332:903] Mohammed

 

  • Convert string to integer 

int value =  [textbox1.text intvalue];

 

  • Convert integer or decimal to string to show in text field

CtrlLbl_Equal.text =  [NSString stringWithFormat:@"%d", 1234];


  •  Use NSMutableDictionary for store one table name without initializing the object

 NSMutableDictionary *DicObj = [NSMutableDictionary dictionary];

 [DicObj setObject:@"Mohammed" forKey:@"FirstName"];

 [DicObj setObject:@"Al-Atari" forKey:@"LastName"];

 NSLog(@”%@” , DicObj);

 Output:

 2012-05-03 20:36:41.401 Project_7[75304:903] {

    Age = 23;

    FirstName = Mohammed;

    LastName = “Al-Atari”;

 }

 

  • Generate a random number 

int intrnd;

intend = arc4random%10 // will generate a random # from 0 – 9

  • Use UiAlertView for showing a message box

UIAlertView *Alert =  [[UIAlertView alloc]

initWithTitle:@”Colors”

message:@”Please choose a color”

delegate:nil

cancelButtonTitle:@”Cancel”

otherButtonTitles:@”Red”, @”Blue”, @”Green”, nil];

[Alert show];

[Alert release];

  • Use UiAlertView for showing a message box and get the selected value

implement UIAlertViewDelegate on ViewController interface

@interface ViewController : UIViewController<UIAlertViewDelegate>

- (IBAction)BtnChooseColor:(id)sender {

    UIAlertView *Alert =  [[UIAlertView alloc]

                           initWithTitle:@”Colors”

                           message:@”Please choose a color”

                           delegate:self

                           cancelButtonTitle:@”Cancel”

                           otherButtonTitles:@”Red”, @”Blue”, @”Green”, nil];

    [Alert show];

    [Alert release];

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    //NSLog(@”%i”, buttonIndex);

    switch (buttonIndex) {

        case 1:

            self.view.backgroundColor =  [UIColor redColor];

            break;

        case 2:

            self.view.backgroundColor =  [UIColor blueColor];

            break;

        case 3:

            self.view.backgroundColor =  [UIColor greenColor];

            break;

        default:

            break;

    }

}

  • Use UIActionSheet for showing a message box

 UIActionSheet *sheet =  [[UIActionSheet alloc]

                             initWithTitle:@”Colors”

                             delegate:self

                             cancelButtonTitle:@”Cancel”

                             destructiveButtonTitle:@”Delete”

                             otherButtonTitles:@”Red”, @”Blue”, @”Green”, nil];

    [sheet showInView:self.view];

    [sheet release];


Apr 1 2012

Kuwait international Bank has become the first Kuwait bank to launch interactive mobile financial services through Zain’s new unstructured supplementary service data (USSD) technology (*866#).

Mohammed Al-Atari

Oh … Finally; I lunch Al-Dawli Connect banking service for Kuwait International Bank (KIB) through Zain USSD Technology. Zain’s USSD technology allows interactive communication between the bank and a customer using their mobile phone.

Unlike SMS banking, the USSD technology allows customers to conduct secure real time transactions through their mobile phones, and get information on a broad range of banking services offered by KIB.

Available for all Zain prepaid and postpaid customers free of charge, the service is accessible through any mobile phone (non-smart phone also) and does not require any special downloads or software installations, It is like having a KIB branch on your mobile phone.

Al-Dawli Connect mobile banking allows customers to perform a number of banking functions, including transfers between a customer’s accounts, credit card inquiry, Finance inquiry and it will have zain bill payments.

Only Dail *866#