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

daihovey picture daihovey · Mar 30, 2010 · Viewed 7.2k times · Source

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

Answer

Mihir Mehta picture Mihir Mehta · Mar 30, 2010

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];