Saturday, 16 April 2016

Add multiple buttons to a UITableView cell on swipe

When we want to add some action to a table view cell when swipe , Apple provides the methods to deal with it. But by default Apple gives only one button when we swipe to left, a delete button . But what if we want more buttons like a mail app in iOS 8.

In iOS8 Apple provides this functionality in form of edit actions and UITableViewRowAction.

To use UITableViewRowAction we have to implement a delegate method of UITableView:

-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    //return an array of UITableViewRowAction instances
}
In this method we create the UITableViewRowAction instance like this:

UITableViewRowAction *delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
       
        //call method to perform action when user tap on delete button   
    }];

The above code will create a delete instance and provide handler block which will be executed when user tap on the targeted  UITableViewRowAction instance.

Like this we can create multiple instances of the UITableViewRowAction  and finally return an array of UITableViewRowAction instances.

How that's simple it is to add multiple button to a table view cell on swipe.But to enable user to perform action on these multiple buttons we have to enable users to allow editing on the table view cell. To do this, implement these two table view delegate methods:

To allow editing on cell, implement:

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

It is necessary to override this second method, even though we can leave it blank. If method is not present the action won't show up on swipe.

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //to do when commiting editing
}

Friday, 18 March 2016

Custom layout for Collection View

UICollectionView provides a way in which we can arrange our data in a grid . Some times we may encounter a problem where we want to design the grid in our way , for example we may want to show the layout of the collection view cell based on the content size of the data we want to display in the collection view. In this way we have to define our custom layout for the each cell based on the content size of the data.


Collection view layouts are subclasses of  the abstract UICollectionViewLayout class. They define the visual attribute of each item in your collection view.

The individual attributes are instances of  UICollectionViewLayoutAttributes and contain the properties of each item in your collection view, such as the frame or opacity of the item .

To customize the layout of the the collection view we have to understand how collection view and layout object works together. Collection view layout process is a collaboration between collection view and the layout object.
When collection view needs some information about layout, it asks your layout object to provide it by calling certain methods in a specific order:

1) prepareLayout
2) collectionViewContentSize
3) layoutAttributesForElementsInRect:

Your layout subclass must implement above mentioned methods:

1) prepareLayout(): It is called when a layout operation is to take place. Here we make calculations to define the collection view size and and the position of the items.

2) collectionViewContentSize(): In this method we return the  height and width of the entire collection view.

3) layoutAttributesForElementInRect(_:) In this method we return  the layout attributes for all the items in a given rect as  UICollectionViewLayoutAttributes .

Friday, 11 March 2016

Working with UIAlertController

UIAlertView and UIActionSheet are deprecated in iOS8. Now to show alert , we use UIAlertViewController which gives a more functionality to show alert. With this single class, we can create alert and add multiple actions to it. Each action has a completion handler called when user tap on that action button .

A UIAlertController objects displays an alert message to the user. This class replaces the UIAlertView and UIActionSheet  classes for displaying alerts. Although UIAlertController is a subclass of the UIViewController but it does not support subclassing  further .

UIAlertController is initialized with a title, message, and whether it prefers to be displayed as an alert or action sheet. Alert views are presented modally in the center of their presenting view controllers, where as action sheets are anchored to the bottom. Alerts can have both buttons and text fields, while action sheet only support buttons.


Creating an Alert Controller :

UIAlertController *alert = [UIAlertController  alertControllerWithTitle:@"My Alert"
                                                 message:@"This is an alert."
                                                 preferredStyle:UIAlertControllerStyleAlert];




title
The title of the alert. Use this string to get the user’s attention and communicate the reason for the alert.
message
Descriptive text that provides additional details about the reason for the alert.
preferredStyle
The style to use when presenting the alert controller. Use this parameter to configure the alert controller as an action sheet or as a modal alert.

When configuring an alert with the UIAlertControllerStyleAlert style , we can also  add text fields to the alert interface. The alert controller provide a block for configuring  text fields prior to display. The alert controller maintains a reference to each text field so that we can access its value later.

We can associate actions with  alert controller to give the user a way to respond. Actions are displayed as buttons in the alert . The action object provides the button text and the action to be performed when that button is tapped.

Creating an Action:

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK"
                                               style: UIAlertActionStyleDefault
                                            handler: ^(UIAlertAction * action) {
                                           
                                          }];

[alert addAction: okAction];
[self  presentViewController:alert animated: YES  completion: nil];


Add TextFields:

-(void)addTextFieldWithConfigurationHandler:(void  (^)
  (UITextField * textField) ) configurationHandler

Calling this method adds an editable text field to the alert. We can call this method more than once to  add additional text fields. The text fields are stacked in the resulting alert.We can add a text field only if the  preferredStyle  property is set to UIAlertControllerStyleAlert.


References:

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/#//apple_ref/occ/instm/UIAlertController/addTextFieldWithConfigurationHandler:

   

Friday, 4 March 2016

Get the height of the text in textview/textfield

Some times we may have need to set the height of the textfield or textview based on the text size contained in the textfield or textview. 

Before iOS 7, we used sizeWithFont: method  to get the desired height, but now it is deprecated. So to get/set the height of the textview/textfield based on the text to be stored in it, we use:

For example: We have textView to show some text . The text length may be different. What if we have some other view there just touching  the  lower left corner of the text field. Though textView can contain different text say one line or multiple lines of text.  Then the position of that other view depends on the height of the textView.

//get the rect size of the counting text

  //set the attribute of the text, like font size and family and some other option
 NSDictionary *attributes  =@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica Neue" size:14]};



CGRect rect = [[text to be stored in view] boundingRectWithSize:CGSizeMake(txtFieldWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];

Now from rect, we can extract the height and make the changes in the frame of the textfield so that textfield has the proper height to contain the text.
So we have approach like this:

 First calculate the text height to be stored in textView using above mentioned method. Then change the frame of the textView to adjust the text in it. After that also make some changes in the frame of the other view so that it places just touching the lower left corer of the textView.





Friday, 26 February 2016

Working with blocks

Block is same as the c function.The basic idea of a block is to treat a small piece of code as if it were a value. The piece of code can then be passed as a parameter in messages or assigned to a variable.

^{NSLog(@"It is a block literal); }


The use of blocks often involves nothing more than including code in block literal form as parameters for methods (designed to take blocks) that are part of the iOS API.
A block is the only object that begins its life in stack, rather than heap memory.A very effective use of a block literal is to deal with a callback. 

Creating a block variable:

int  (^sum)(int, int) = ^(int a , int b){
 return a+b;
}

This  is a simple block definition . It takes two parameters  of type int and return one int parameter.
To call this block as:

int c = sum(3,5);
NSLog(@"sum is %d",c);

When a block is created, it will capture, or close around, the values of those variables that the code has referred to and that are in the same lexical scope of the block. Essentially, a snapshot is taken of these values. These values are preserved in the memory allocated for the block and cannot later be changed. It is said that these variables are "captured by value".
To change the this behavior(to use the latest values of the variable), we can declare these variable as 
__block storage specifier.

__block int z =5;

Block as method parameter: We can also use block as method parameter to use call back. For example:
 
-(void)validateString:(NSString *)str onCompletion:(void(^)(bool isValidate))completion{
          
 if(str.length ==0){
    completion(NO); //call block
}else{
    completion(YES);
}
}

Now call this method as 

[self validateString:@"iOS" onCompletion:^(bool isValidate){
    
if(isValidate){
 //str is valid, handle it
}else{
  // str is not valid, handle it here
}       
   
}];

That's it. Though the use of block seems some complex at first time but once we get used to, it is very easy to use block in our code. With the help of block we can make our code simple, readable and easy to maintain .

Friday, 19 February 2016

iOS app life cycle

We can categorized iOS life cycle mainly in two category:

1) The App launch cycle
2) View Controller life cycle

The launch cycle states different app states in which an iOS app can transit in its life . The different app states are as:

1) Not running
2) Inactive
3) Active
4) Background
5) Suspended

When an app goes from one states to another it is called app delegate methods. These methods provides a way  to take appropriate action  on app state changes. These methods are as:

application:didFinishLaunchingWithOption: called when app launches first time.

applicationDidBecomeActive: called after app enters in foreground.

applicationWillResignActive: called before an app enters in background.

applicationDidEnterBackground: called after app enters in background. In this state an app may be suspended at any time.

applicationWillEnterForeground:called when app changes state from background to foreground. But app is not yet active

applicationWillTerminated: This method is not called when an app is suspended or user terminated the app explicitly.


View Controller life cycle:

viewDidLoad: called only once  during initial load of the interface.

viewWillAppear:This method is called just  before views appears or renders on screen every time when you   navigate or switch between different views.

viewDidAppear: called when views completely.  Here we can handle UI or functionalities.

viewWillDisappear: called before view is removed or any animation is configured.

viewDidDisappear:called when view is removed.

Friday, 12 February 2016

Property Attributes in Objective C

Property attributes gives the functionality to define a way in which the property works. With attributes we can change the way property works. These property attributes can be categorized into three category.

1) Atomocity
       atomic and nonatomic
2)Access
       readonly and readwrite
3) Storage
      strong, weak , assign and copy


Atomic(default):
       An atomic property guaranteed that you will get a valid data when you try to read from it. It does not make any guarantees what the data might be but you will get the good data not just junk memory. This is used when multiple thread or processes pointing to same variable and reading and writing simultaneously.

Nonatomic:With it you lose the guarantee that you will get good data every time . When you try to access in the middle of the write, you could get back the garbage data.


ReadOnly: It makes the property read only that means no setter for it at all.

ReadWrite(default): It is the flip side of the readonly. Allows both read and write.

Strong(default): It means you have a reference to an object and you will keep the object alive. As you hold the reference to the object , the object will not  be deallocated and released back to the memory.

Weak: It gives the reference to the object so that  you can talk to the object but you can not keep it alive. If object's reference count goes to zero, the property becomes nil automatically.

Assign:It is used for primitives.

Copy:It works well with all kinds of mutable objects If you set a copy property, instead of just setting both references to the same object, what it actually does is makes a copy of the object you are setting and then sets the property to that copy.