This code uses a delegate
[[[UIAlertView alloc] initWithTitle: # "Error" message: # "You can leave the text blank" delegate: self cancelButtonTitle: # "Quit" otherButtonTitles: # "OK", nil] show];
The delegate in question is:
(void) alertView: (UIAlertView *) alertView clickedButtonAtIndex: (NSInteger) buttonIndex {
My question is this:
How do you autocomplete for Xcode delegate methods?.
In Eclipse you can do from the Source menu option Generate Delegate methods.
Regards and thanks!
Your class should support UIAlertViewDelegate protocol
#interface YourClass : NSObject
<
UIAlertViewDelegate
>
start typing "dash" - you'll see all supported method included UIAlertViewDelegate methods. To reduce the autocomplete methods list: type -alert
Simply use "Go to definition" to see all "UIAlertViewDelegate" methods. Start typing one of the following methods in your XCode
If method under #required section you have to implement it in your delegate object otherwise you'll get warning.
If under #optional methods you don't have to implement any of these methods (you won't get warnings), but in most cases you should do this to let it work as you expect.
try press esc in keyboard, Xcode : Version 11.3.1 (11C504)
If the method is not required, you won't get any warning about unimplemented methods.
XCode and Eclipse are different.
In second thought, u can use this trick
Another choice, you can use appcode instead of xcode, "Generate" feature can be found.
In Xcode you can create your own class with the custom component as per your need and create a delegate methods in side that for responding like people uses for parsing and AsyncImageView and etc..
you can visit here for Async class demo with the respoct of this you can write your own class for UIAlertView
Thank you very much to all.
In this case all "#protocol UIAlertViewDelegate" methods are under #optional.I understand that in this case it is not necessary my class should support UIAlertViewDelegate protocol because all methods are under #optional.
The solutions is:
right-click on UIAlertViewDelegate or UIAlertView, click on "Jump to definition", then you can see the #protocol. Inside you can see methods with #optional and #requiered. If method under #required section you have to implement it in your delegate object otherwise you'll get warning. If under #optional methods you don't have to implement any of these methods (you won't get warnings)
Thank you again
This is what I do for delegates that I use frequently.
Find the required methods and optional ones that you will always implement by accessing the UIAlertViewDelegate definition.
Add empty implementations for these methods.
Add a snippet with the shortcut being the delegate name and the content being my empty implementations (select then drag and drop to snippet library)
Next time I use that delegate all I need to do is to start typing my shortcut (Ex: AlertViewDelegate to auto-insert my empty implementations
Bonus:
I also sometimes add in some default code in the implementations that is always going to be there, example:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString* title = [alertView.title lowercaseString];
if (buttonIndex != alertView.cancelButtonIndex) {
if ([title isEqualToString:<#action title#>]) {
<#statements#>
}
}else{
<#cancel code#>
}
}
Related
Ok, so am kind of new to programming for iPhones and Objective C and Cocoa Touch, but cannot figure this out. There is a lot of information when the delegate is self, but I am trying to send it to another file.
Here is some code...
I have the following method (I would normally call it a function but I think method is the correct terminology) in appDelegate.m
[GameUtil showAlertViewWithTag:1 delegate:self withTitle:#"" message:msg cancelButtonTitle:#"New game" otherButtonTitles:#"Main Menu", nil];
In GameUtil.h and .m I have showAlertViewWithTag set up properly and it is working fine.
The problem I am having, is with the delegate:self part ... as it stands, when a button on alertview is clicked, it will go to -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex that is located in my appDelegate.m(which of course is expected when using self as the delegate).
But what I want it to do when a button is clicked is send it to -(void)alertView:.. in a different file called playField.m.
I have the alertview setup correctly in playField.m (have tested by calling alertview with delegate:self from within playField.m).
Basically I am stumped on how to tell the alertview in appDelegate.m to use a delegate that is in playField.m instead of delegate:self.
I think I need to #import playField.m or .h at into appDelegate.m but beyond that I am lost with all the #protocol #interface stuff.
Baby steps would be very helpful, like "You need to put XYZ at the top of your playField.h and ABC at the top of playField.m".
I am hoping this is fairly simple to implement, and that I am just missing a few simple things.
Thanks in advance :)
First, even though the outcome of this is going to be that the delegate method will be in another file, you should rather think of it as "the delegate is another object".
In your case, the delegate object is self, that is the App Delegate object. (because you're writing this inside the app delegate implementation)
In order for the delegate to be another object, you first need to have an instance of that object accessible in your app delegate.
I am assuming your playField files hold a playField class. (I would have called it PlayField btw, classes are usually capitalized, and the objects (an instance of a class) are not, although it works either way.
Somewhere, you need to instantiate a playField object, have you done that somewhere? This would look like:
playField *pf=[[playField alloc] init];
If you haven't done so, then do it above the showAlertViewCode.
In order for the classes to be available in your appDelegate, add:
#import "playField.h"
on top of your appDelegate file.
then in your alertview code, replace self by the object you just created:
[GameUtil showAlertViewWithTag:1 delegate:pf withTitle:#"" message:msg cancelButtonTitle:#"New game" otherButtonTitles:#"Main Menu", nil];
Now your delegate is the pf Object, which is an instance of playField, therefore the delegate method will be called from your playField.m implementation file.
Hope this makes sense!
[GameUtil showAlertViewWithTag:1 delegate:self withTitle:#"" message:msg cancelButtonTitle:#"New game" otherButtonTitles:#"Main Menu", nil];
In the line above, change delegate:self to delegate:GameUtil or delegate:self.GameUtil if you have used property.
And in the GameUtil.h file, add <UIAlertViewDelegate> to the end of #Interface line.
I'm using a library called Chute to manage photo selection (from photo library, facebook, etc.).
I'm trying to implement a UIAlertView whenever selectionCount >= 20. The problem is, the Chute library handles alertView protocols in its own way, meaning some file is trying to execute
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{}
whenever my alert with buttons is displayed, which crashes the app. What should I do to work around the current method? I tried to subclass UIAlertView into a new custom class but I'm not sure where to implement the UIAlertViewDelegate or its protocol. Not sure if this is right either. Any help appreciated, thanks.
alertView: clickedButtonAtIndex: gets called on UIAlertView's delegate.
I'm trying to make my own version of UITableViewController in a UIViewController (for more customization). So in my superclass, I'm implementing both the delegate and datasource and setting the UITableView object delegate and datasource properties to "this". Only problem is I get a compiler warning complaining that I haven't implemented the mandatory dataSource and delegate methods. I'd like to be able to implement these methods when I subclass my custom UITableViewController.
Is there a neat way to make these warnings go away, or is the only way for me to put empty versions of the mandatory delegate methods, and then override them in the subclass? Is this bad practice? Anyone have any insight on how Apple accomplishes this with there UITableViewController class?
I use this pattern for “abstract” methods:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
[self doesNotRecognizeSelector:_cmd];
abort();
}
The _cmd variable is the selector of the current method. It's automatically provided, just like self.
You have to call abort() because the compiler knows that abort() doesn't return, but it doesn't know that about doesNotRecognizeSelector:.
At least in iOS 5, the UITableViewController methods aren't empty, because UITableViewController supports loading predefined (static) rows from a storyboard.
I have a UIActionsheet with several buttons setup. How would I go about adding a undo function? I'm using UITextfields
Set the appropriate UIActionSheet button to call the undo method on the desired UITextField's NSUndoManager object. For all UITextField and UITextView objects in iOS 3.0 and higher, there is an undoManager object that can be accessed and used to manage undoing and redoing of operations. The necessary code would go something like this:
(UIActionSheetDelegate method implementation)
- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == /*undo index*/)
[yourTextField.undoManager undo];
}
EDIT: I suggest you look at the Apple documentation for Undo Managers, it should help you further with this should you need it.
Have you considered Shake to Undo? An Undo alert comes up if you enter some text in a UITextField/UITextView & shake your iPhone.
I am building a class that handles NSURLConnection requests. To allow other classes to use this class, I would like to allow the main class to call a delegate when connectionDidFinishLoading is fired.
I've looked through lots of documentation, but I can't find anything that gives any clear examples, and the code that I have doesn't call the delegate for some reason. The code I have so far is (code not relevant removed):
Interface:
#interface PDUrlHandler : NSObject {
id delegate;
}
- (void)searchForItemNamed:(NSString *)searchQuery;
#property (nonatomic, assign) id delegate;
#end
#interface NSObject (PDUrlHandlerDelegate)
- (void)urlHandler:(PDUrlhandler*)urlHandler searchResultsFinishedLoading:(NSDictionary *)resultData;
#end
Implementation:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(#"Fininshed Loading...");
resultData = [self parseJSON:jsonData];
if(delegate && [delegate respondsToSelector:#selector(urlHandler:searchResultsFinishedLoading:)]) {
NSLog(#"Delegating!");
[delegate urlHandler:self searchResultsFinishedLoading:resultData];
} else {
NSLog(#"Not Delegating. I dont know why.");
}
}
The delegate within the other class:
- (void)urlHandler:(PDUrlhandler*)urlHandler searchResultsFinishedLoading:(NSDictionary *)resultData;
{
NSLog(#"Delegating!!!!");
}
My first thought was you might not have set the delegate, but you have. Other than that, the code looks correct. Can't see anything wrong. Did you try and put a breakpoint at the place where you are checking whether your delegate responds to a selector? Could be that the delegate value was not retained and became nil. Make sure your delegate is not nil and has the correct object.
Also are you sure the connection is asynchronous? Synchronous connections will not call the connectionDidFinishLoading method
Turns out I forgot to set the delegate:
[currentHandler setDelegate:self];
needed to go after the line that makes the initial call to the PDUrlHandler.
For anyone interested in seeing an example of this the apple sample application NSURLCache implements a simple delegate around an NSURLConnection in NSURLCacheConnection.m
The sample app is available through the apple developer connection here:
http://developer.apple.com/iphone/library/samplecode/URLCache/index.html
I found it pretty useful.
You're on the right track, the way you're implementing the delegate pattern looks okay. The reason it's not being called is because you're using the wrong method signature in respondsToSelector; you have humidorServer:searchResultsFinishedLoading: when you actually want urlHandler:searchResultsFinishedLoading:.
Could it be the semi colon at the end of the method name in the delegate (bottom code sample on the far right)? If the delegate is set and the -connectionDidFinishLoading: method is being called then I can't see anything wrong
Since you tagged this with "iphone", I assume you're working on an iPhone app and don't need to support OS X pre-10.5. In Objective-C 2.0, Apple suggests you use formal protocols (using #protocol) with optional methods instead of informal protocols. Here's the relevant text:
Being informal, protocols declared in
categories don’t receive much language
support. There’s no type checking at
compile time nor a check at runtime to
see whether an object conforms to the
protocol. To get these benefits, you
must use a formal protocol. An
informal protocol may be useful when
all the methods are optional, such as
for a delegate, but (on Mac OS X v10.5
and later) it is typically better to
use a formal protocol with optional
methods.
(source)