views:

61

answers:

1

Ok so I've downloaded an open source project that I'm looking at customising for personal use (unless it turns into a great project in which case who knows) but in either case I'm having some difficulty.

From a starting point I'm finding creating UI elements using progmatic methods rather difficult, just can't find a good tutorial out there (if anyone has any suggestions it would be much appreciated!)

So I've come to the conclusion that maybe my best method is to take the view that I wish to modify, recreate it in Interface Builder, then add it to the previous view, the problem here is that I just can't seem to get my head around how to do this. If anyone wants to see the code I can provide it but it's based on the RemotePad open source project (easily googled) and I'm looking at replacing the TapView elements - really all I need to do is add a fourth button under the three mouse buttons but it's just lost me.

I suppose really what I'm asking is what's the best way for me to go about adding this fourth button? Ideally the button should be 'skinable' i.e. should be in the form of an image that can have a highlighted mode applied.

+3  A: 

This should actually be fairly easy to do.

  1. First, you need to set up the existing view controller (the one associated with the view where you want to add your button) with an IBOutlet for your new button. So you add something like:
    @property (nonatomic, retain) IBOutlet UIButton *myFourthButton;
    
  2. Next, create the nib file with Interface Builder. Start with an empty IB file and add your button to it. You also need to set the File's Owner to be an instance of the view controller class. Then connect the File Owner's myFourthButton outlet to your new button. Save the IB file.

  3. Now you need some way to load this NIB file when the view controller is created. I would suggest doing this in the view controller's viewDidLoad: method by calling:

    [[NSBundle mainBundle] loadNibNamed:@"yourNibFile" owner:self options:nil];
    
  4. The button from the NIB file should now be connected to your myFourthButton outlet, now you just need to add it to the view and position it. Below I add it to the view controllers main view. However, there may be a subview that you should add it to instead (depends on how the original view is set up). Again, I would put this code in viewDidLoad: after all of the existing code to set up the view programmatically (or in another method if that code is elsewhere).

    [self.view addSubview:myFourthButton];  
    
    
    CGRect frame = myFourthButton.frame;
    frame.origin.x = 100;
    frame.origin.y = 100;
    myFourthButton.frame = frame;
    

    When you need your button to actually respond to a tap event, you can connect it to your view controller using an IBAction and Interface Builder just as you would expect.

Tim Isganitis
This sounds like it makes sense to me. So essentially I'm keeping the current .h and .m file and using a NIB to 'cheat' making buttons? When I want to add an IBAction to the button do I just end up doing this exactly the same as if I'd added a new class with .xib? I'll give this a go tomorrow evening (I'm working on a big project right now and I've had my fill of Xcode for the night!) I'll mark up as an answer as appropriate if it works for me :-)
David26th
Correct, you are using the existing .h/.m files for the view controller. This isn't really "cheating," there are plenty of instances where it makes sense to mix interface elements created programmatically and via IB (table view cells are one good example). Basically, instead of instantiating and configuring the UIButton yourself, you are letting the nib loader do that work for you. At the end of the day, I think it makes sense to work with what you are most comfortable with.
Tim Isganitis
In terms of adding an action, you would first declare the IBAction in the view controller's .h/.m files, then drag a connection from the UIButton to the File's Owner (the view controller). Good luck!
Tim Isganitis
Sounds grand - can't wait to try it out - I only phrase it as 'cheating' in comparisson to fully declaring it all in code :) Meant in an affectionate sense lol
David26th
Ok I've given this a try and I'm getting 3 !'s [[NSBundle mainBundle] loadNibNamed:buttons owner:self options:nil]; gives 'buttons' undeclared[buttons.view addSubview:powerOne]; gives 'powerOne' (the name of the button in question) undeclared.Actually two seconds thought made me realise that I should synthesize powerOne in the implementation file, that said I'm still struggling on buttons undeclared message, suspect I have to declare in the header and synthesize in the implementation but I'm not sure how the declaration should look for this@property (nonatomic, retain) UIView *buttons;
David26th
Ok tried that - definitely not the way...any thoughts?
David26th
The argument you pass to loadNibNamed: should be an NSString specifying the NIB file name. So, if you have buttons.nib, the argument would be @"buttons" (you don't need the extension in the argument string). I'm editing my instructions above to make this clear.
Tim Isganitis
Ah thanks, I'll give that a go.
David26th
Second, the "File's Owner" in the NIB is just a placeholder. When you load the NIB, the object you specify as the owner: (in this case "self") is substituted for the "File's Owner" in the NIB. So, if you have an IBOutlet from the File's Owner to a button, then the property corresponding to that IBOutlet in the owner object (i.e. your view controller) is initialized with the object from the NIB. In short, after you load the nib, you should access the button loaded from the nib file using the property you declared in the view controller's .h file. Something like: self.powerOne .
Tim Isganitis
Right, now not to be too dense but this line[viewController.view addSubview:myFourthButton]; is giving me feedback that viewController is undeclared - this works if I change to TapView (the name of the .m) or buttons (the NIB) - which should it actually be? Might end up coming back to you on that last comment yet.
David26th
If you are in the .m file of the view controller, use self.view.
Tim Isganitis
That did it for getting the button in THANK YOU. I might need a bit of help with the initialisation of the IBAction but right now - it's time for food! :-D
David26th
Just chucking this in here to see if I can get a response rather than a new thread but....-(IBAction)powerOneTap { [appc send:EVENT_ASCII with:063 time:3.2];}This works, works beautifully. Puts the number 3 on my screen just as if I pushed the key on my laptops keyboard. However what could I do to apply say a Command modifier to it, to make it as if I pushed Command + 3I know I've seen samples before but can't find them now I need them :S Anyone got any great ideas. This is going really well at the minute.
David26th