Thursday, 5 June 2014

Add UIBarButtonItem on NavigationBar

a) UIBarButtonItem *btn = [[UIBarButtonItem alloc]
                                initWithTitle:@"Add"
                                style:UIBarButtonItemStyleBordered
                                target:self
                                action:@selector(buttonAddClick)];
    
    self.navigationItem.rightBarButtonItem = btn;



b) UIBarButtonItem * btn = [[UIBarButtonItem alloc]
                                  initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                  target:self action:@selector(buttonAddClick)];
    
    self.navigationItem.rightBarButtonItem = btn;



c) UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];

    btn.frame = CGRectMake(0, 0, 30, 30);

    [btn setImage:[UIImage imageNamed:@"plus.png"] forState:UIControlStateNormal];

    //[btn setImage:[UIImage imageNamed:@"logout-hover"] forState:UIControlStateHighlighted];

    [btn addTarget:self action:@selector(buttonAddClick) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView: btn];

    self.navigationItem.rightBarButtonItem = rightBarButtonItem;

Monday, 2 June 2014

What is the difference between @class and #import ?

@class is used when you need to know the name of a class in a particular file, but you don't need to know any details about the class (its methods, for example). #import is used when you actually need to use the class (i.e., send it a message).

@class is called a forward declaration. You're basically telling the compiler that the class exists but not anything about the class. Thus, it doesn't know stuff like its superclass and what methods it declares.

For example, 
if you're declaring instance variables in a header file, you can use @class to declare an instance variable of a certain type:


@class MyOtherClass;

@interface MyClass : NSObject
{
    MyOtherClass *myIvar;
}
@end



Since you're not using myIvar yet, you don't need to know anything about it except that the type MyOtherClass exists.

However:


#import "MyOtherClass.h"

- (void)doSomething
{
    [myIvar doSomethingElse];
}


In this case, you're sending the doSomethingElse message to myIvar; the compiler needs to know that instances of MyOtherClass define this method, so you have to import the header file or the compiler will complain.




Why worry about this?



It mostly has to do with dependencies. When you #import file A into file B, file B becomes dependent upon file A -- that is, if file A changes, you'll have to recompile file B. If you use @class in file B, file B is not dependent on file A, and thus doesn't need to be recompiled when file A changes -- so if you're just declaring a type and not actually dependent upon the implementation of file A, you can save yourself compilation time by not #importing file A.

Difference between Copy and MutableCopy

The difference between 'copy' and 'mutableCopy' can be simply understood with polymorphism in Object Oriented Programming concepts.

We will take the example of Array in objective-C. MutableArray is the extension of NSArray class. Therefore, all the methods available in NSArray is available in NSMutableArray, but the additional methods present in NSMutableArray is not known to NSArray class.

Now moving ahead, the copy method on an NSArray will return an object of type NSArray(The array that can not be modified). And mutableCopy method will return an object of mutable type (The array that can be modified).

Now, there can below cases :

Case 1:

    NSArray *arr1=[NSArray arrayWithObjects:@"A",@"B",@"C", nil];
    NSArray *arr2=[arr1 copy];

    NSLog(@"arr1:%@",[arr1 description]);
    NSLog(@"arr2:%@",[arr2 description]);

In this case an NSArray object is returned and is received in an NSArray object.
So, the array received can not be modified.

Therefore, we will not be able to use below statement
[arr2 insertObject:@"Z" atIndex:0];


Case 2:

    NSArray *arr1=[NSArray arrayWithObjects:@"A",@"B",@"C", nil];
    NSArray *arr2=[arr1 mutableCopy];

    NSLog(@"arr1:%@",[arr1 description]);
    NSLog(@"arr2:%@",[arr2 description]);

In this case an NSMutableArray object is returned and is received in an NSArray object.
Since the receiver object is of type NSArray, it doesn't know the methods present in NSMutableArray, arr2 will not be able to use any of the methods of NSMutableArray.
That is, the method mutableArray will make no sense in this scenario.

So, we will not be able to use below statement
[arr2 insertObject:@"Z" atIndex:0];

Case 3:

    NSArray *arr1=[NSArray arrayWithObjects:@"A",@"B",@"C", nil];
    NSMutableArray *arr2=[arr1 copy];

    NSLog(@"arr1:%@",[arr1 description]);
    NSLog(@"arr2:%@",[arr2 description]);

In this case an NSArray object is returned and is received in an NSMutableArray type object. The receiver arr2 is now pointing to an object address that is of type NSArray. However arr2 has the additional methods than NSArray, it will not be able to use those methods coz the pointed object NSArray does not know the additional methods present in arr2(NSMutableArray).

Hence, we will not be able to use below statement
[arr2 insertObject:@"Z" atIndex:0];


Case 4:

    NSArray *arr1=[NSArray arrayWithObjects:@"A",@"B",@"C", nil];
    NSMutableArray *arr2=[arr1 mutableCopy];

    NSLog(@"arr1:%@",[arr1 description]);
    NSLog(@"arr2:%@",[arr2 description]);
    [arr2 insertObject:@"Z" atIndex:0];

In this scenario, the receiver(arr2) of type NSMutableArray receives an object of type NSMutableArray. Therefore, the receiver knows the additional methods of NSMutableArray as well as the object that is being pointed by receiver(arr2).

And finally below statement will work like charm,
[arr2 insertObject:@"Z" atIndex:0];

Log before insertion will be:
arr1:(
    A,
    B,
    C
)
arr2:(
    A,
    B,
    C
)
And log after insertion will be:
arr1:(
    A,
    B,
    C
)

arr2:(
    Z,
    A,
    B,
    C
)




Hope above description helped you.

Simply:

  1. mutableCopy always returns a mutable result.
  2. copy always returns an immutable result.