Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you hard code IBActions and IBOutlets, rather than drag them manually in Interface Builder?

Is it possible to to hard code the IBActions and IBOutlets in code, instead of drag-connecting them in Interface Builder?

like image 1000
daihovey Avatar asked Mar 30 '10 10:03

daihovey


People also ask

What is the difference between IBOutlet and IBAction?

An IBOutlet is for hooking up a property to a view when designing your XIB. An IBAction is for hooking a method (action) up to a view when designing your XIB. An IBOutlet lets you reference the view from your controller code.

What does IB stand for in IBOutlet?

IB stands for interface builder, as you connect objects via the interface builder .

How do you delete an IBOutlet connection?

Select the view on the storyboard and then click the Connections Inspector. Then you can click the little x to remove an outlet reference.

What is Xcode IBOutlet?

The type qualifier IBOutlet is a tag applied to an property declaration so that the Interface Builder application can recognize the property as an outlet and synchronize the display and connection of it with Xcode. An outlet is declared as a weak reference ( weak ) to prevent strong reference cycles.


2 Answers

Yes it is poosible...

sample code

UIButton *btnDetail = [UIButton buttonWithType:UIButtonTypeRoundedRect] ;

        [btnDetail setFrame:CGRectMake(250.0f, 15.0f, 65.0f, 20.0f)] ;
        //btnDetail.backgroundColor = [UIColor grayColor];
        [btnDetail setTitle:@"Detail" forState:UIControlStateNormal];
        [btnDetail setTitleColor: [UIColor redColor] forState: UIControlStateNormal];
        [btnDetail.titleLabel setFont:[UIFont fontWithName:@"Verdana" size:12]];
        [btnDetail setBackgroundColor:[UIColor clearColor]];
        [btnDetail sizeThatFits:btnDetail.frame.size];
        [self.view addSubview:btnDetail];
//IBAction
        [btnDetail addTarget:self 
                   action:@selector(ShowSavingAccountDetail:)
         forControlEvents:UIControlEventTouchUpInside];

        [btnDetail setImage:[UIImage imageNamed:@"btn-detail.png"] forState:UIControlStateNormal];
like image 188
Mihir Mehta Avatar answered Oct 21 '22 21:10

Mihir Mehta


The concept and sole purpose of IBAction and IBOutlet is to provide Interface Builder with means to connect the xib with your code.

If you don't want to use Interface Builder with your code, you don't need IBAction or IBOutlet, you need them ONLY to use objects (buttons, textfields, etc.) that were instanciated in your xib from your classes.

With that said, mihirpmehta's answer is the correct way to programmatically add UI elements to your view and add actions to them.

like image 32
Marin Todorov Avatar answered Oct 21 '22 21:10

Marin Todorov