Benjamin Wootton
Benjamin Wootton

Reputation: 2107

Xcode / iOS Delegates

If I have a view controller which implements two protocols:

@interface CustomerOperationsViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>

Is there any easy way to create the required callbacks for the protocols? Maybe an Xcode shortcut for implement methods? I'm going to the documentation each time for this.

Related, is it possible to put the delegate into a different file than the file owner? I don't see how to drag from the UI element to a class other than the file owner.

Upvotes: 2

Views: 3928

Answers (2)

Wolfgang Schreurs
Wolfgang Schreurs

Reputation: 11834

Just Command-Click on the delegate method and it will show you the delegate's header, so you don't need to browse the internet (e.g. Apple's Developer Site) for the appropriate docs. Also, in my implementation files I usually write code like this whenever I implement a delegate:

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    // implementation here ...
}

#pragma mark - UITableVieWDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // implementation here ...
}

By Command-Clicking on the pragma mark text (UITableViewDelegate / UITableViewDataSource), the delegate header will be shown as well, less navigation to the header file of the current class to Command-Click the protocol. Just copy and paste the methods you need from the delegate's header.

Finally what's also useful is just start typing in Xcode (any implementation file that conforms to the protocol you want to autocomplete) ...

- tableView 

and press escape. The tableView delegate methods will pop-up and you can select the one you want with tab, it will autocomplete. Same is true for delegates of other objects.

Example

Upvotes: 4

lxt
lxt

Reputation: 31304

Welcome to XCode, one of the more frustrating IDEs out there. There isn't a particularly straightforward way to pre-populate callbacks, although they should come through in CodeSense. You could copy/paste from the header files, but you'll still need to manually edit some stuff.

As to your second question: yes - your delegate doesn't have to be the file owner, but normally if you were setting it to something else you'd do it programatically rather than via IB. Where/what did you want your delegate to be? Another view controller, or something different?

Upvotes: 3

Related Questions