Thursday 14 August 2014

Accessing Elements Added in XIB without doing a IBOutlet Mapping

Probably most of you guys know about this. We can access a view element in a view controller by its tag number. This can be used in XIB's also.

For example:-

If we have added a UIButton in XIB , and we need to change its title dynamically from code. The traditional way of doing is creating a property with IBOutlet and mapping it to the corresponding element. Then change the title using the variable as given below.






But every time creating a property with IBOutlet is not necessary. We can do it the following way.

(Step 1) Add a tag to the corresponding element in the XIB file. Which is shown below in the case of UIButton.






(Step 2) Getting the UIButton element by accessing its tag value as given below.


    UIButton *button = (UIButton *)[self.view viewWithTag:234];
    if(button)
    {
        [button setTitle:@"Submit" forState:UIControlStateNormal];

    }


This can be applicable to most of the UIControl items also element like UILabel, UITableView, UIPickerView, UIView, which ever is having a tag property.






No comments:

Post a Comment