-[UIThreePartButton text]: unrecognized selector - iphone

I've been racking myself on this one for a while, and can't figure it out.
It is an action sheet that bookmarks a page (for a safari like app) When I hit Okay, i get the
-[UIThreePartButton text]: unrecognized selector at instance XXXX
I can't seem to step through where my pain is.
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Bookmarks" message:#"Please enter the site name: \n \n \n" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
alert.center=CGPointMake(160, 50);
alert.tag=1;
UITextField *siteName=[[UITextField alloc]initWithFrame:CGRectMake(20, 70, 245, 30)];
siteName.backgroundColor=[UIColor clearColor];
siteName.borderStyle=UITextBorderStyleRoundedRect;
siteName.autocorrectionType=UITextAutocorrectionTypeNo;
siteName.delegate=self;
siteName.clearButtonMode=UITextFieldViewModeWhileEditing;
siteName.text=[[mainDelegate.sitesArray objectAtIndex:tag] objectForKey:#"webSite"];
siteName.returnKeyType=UIReturnKeyDone;
siteName.tag=2;
[alert addSubview:siteName];
[alert show];
}
}
I realized that it dies on the alert view method below it:
if(alertView.tag==1)
if (buttonIndex==1)
{
if(((UITextField*)[alertView.subviews objectAtIndex:4]).text==#""||((UITextField*)[alertView.subviews objectAtIndex:4]).text==nil)
{
Spefically on the if(((UITextField*)[alertView.subviews objectAtIndex:4])
I have 4 buttons/action on that action sheet, and the rest are good to go...

Do not try to manually traverse the view hierarchy of a UI element that Apple does not otherwise document. Doing something like [alertView.subviews objectAtIndex:4] is dangerous, because you end up having to make a blind assumption of the internal structure of a UIAlertView.
In this case, you're hitting the wrong element (a UIThreePartButton) and crashing your application with an exception. Even if you do get the index correct on where your text field is right now, there's no guarantee that Apple won't change the view number or ordering in a future OS release, suddenly causing all installations of your application to crash.
To prevent this, hold on to a reference to your UITextField so you don't need to probe the view hierarchy on a UIAlertView.
Even better, don't use an alert view for text input, but a custom modal view. Alerts really should be reserved for infrequent error displays or other critical updates. From the iOS Human Interface Guidelines:
The infrequency with which alerts
appear helps users take them
seriously. Be sure to minimize the
number of alerts your app displays and
ensure that each one offers critical
information and useful choices.
As an FYI, you're also leaking your siteName UITextField in the above code, and potentially your UIAlertView.

If you have 4 buttons per action would your objectAtIndex not be 3 instead of 4? Seems like you are getting an out of bounds exception.
Specifically objectAtIndex:4 will generate an exception if there are 4 items in the array since the last valid index is 3.

Related

Is it possible to access UIAlertView using tag?

I hope to access UIAlertView using tag. The codes show below
UIAlertView *a=(UIAlertView *)[self.view viewWithTag:presetTag];
but a returns no object(0x0)
I am looking to find a way to get the pointer to the UIAlertView object that is displayed without creating a reference to it in my UIViewController class that is displaying it. I am creating the UIAlertView and assigning it's tag property a constant non-zero value, then displaying it via show and releasing the UIAlertView reference.
An example where this could come in handy is if I want to hide the alert view based on some other event that is not touching one of the buttons on the alert view. Say a server informs the app that the alert is no longer valid and so I dismiss with button index -1 on the alert view. But, I have no reference to that alert so how can I find it?
Welcome any comment
Thanks
interdev
As the UIAlertView is not part of the application's view hierarchy, the only way to access it would be to store the instance right after you created it, such as in a dictionary so you can later retrieve it.
Something like:
UIStateAlertView *alert = [[UIStateAlertView alloc]
initWithTitle:#"my_message"
message:nil
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles: nil];
alert.tag = kMY_TAG;
[_alertIndex setObject:alert forKey:[NSNumber numberWithInt:kMy_TAG]];
[alert show];
[alert release];
to retrieve the alert:
UIAlertView *alert = [_alertIndex objectForKey:[NSNumber numberWithInt:kMy_TAG]];
When you're done with the view, make sure to remove it from the dictionary:
[_alertIndex removeObjectForKey:[NSNumber numberWithInt:kMy_TAG]];
_alertIndex being a NSMutableDictionary
iPhone SDK: check if a UIAlertView is showing provides a solution, however this is relying on how Apple does its internal affair and could break at any moment; and so should be avoided
UIAlertView creates its own UIWindow, which is not part of your app's main hierarchy of UIViews. Therefore it is not possible to find it using [UIView viewWithTag:].
You must store the pointer to the UIAlertViews you create in order to find them again later.
There is a way to access UIAlertViews in your app (and then you could use tags to verify that it's the one you're looking for), but it is relying on the internal structure of the app and may therefore stop working in future versions of iOS, though it is unlikely. If you are interested, please see this other SO response.
I think it is possible, please check that you are setting a non-zero value to the tag.
Is this what you need? Specifically speaking, you can access each UIAlertView's tag info through its tag property in protocol callback methods.
#protocol UIAlertViewDelegate <NSObject>
#optional
// Called when a button is clicked. The view will be automatically dismissed after this call returns
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
// Called when we cancel a view (eg. the user clicks the Home button). This is not called when the user clicks the cancel button.
// If not defined in the delegate, we simulate a click in the cancel button
- (void)alertViewCancel:(UIAlertView *)alertView;
- (void)willPresentAlertView:(UIAlertView *)alertView; // before animation and showing view
- (void)didPresentAlertView:(UIAlertView *)alertView; // after animation
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex; // before animation and hiding view
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex; // after animation
#end

How to show multiple Alerts one after another in iphone app

Hi
let me explain briefly.
i am developing an iphone application where i have to show multiple UIAlerts one after the another,
but if i simply use multiple [alert show]; they all show up stacking up to each other.
one solution is to show one alert [alert1 show]; then in
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
detect the end of first alert1 then show the second alert by calling [alert2 show]; and so on continue to show other alerts using alertView.
i want to know is there any other way to show multiple alerts in a sequence without the pain of using alertView to detect end of first alert and then show the second one ?
In short, there is no other way. A slight better way might be that you can save the pending alerts in a mutable array and then pop next one (if any) in the delegate so that you don't have to go in a long if-else sequence. You can write your own alert queue in this way.

Iphone: stop execution of code when alert is shown in a viewWillAppear implementation

I have a working application
before the first view is loaded, i put an alert in the viewWillAppear method:
- (void)viewWillAppear
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"MyAppp" message:#"Application will connect to Internet. Continue?"
delegate:self cancelButtonTitle:nil otherButtonTitles:#"No, quit", #"Yes", nil];
[alert show];
[alert release];
}
I can get the clicks on the two button (Yes/No) correctly...
But...I would like code execution to stop and wait for an answer, but instead the code goes on, connects to the internet and retrieves data...
How do I prevent a view to load, based on a user input?
The viewWillAppear is a notification which allows you to complete some stuff before the view is shown, you can't avoid the appearing of the view here. You have to review your implementation.
Just break your one viewWillAppear method into two methods. Don't try to do it all in one chunk of sequential code.
The first method will launch the alert and then just exit/quit/return.
The second method can be called by the alert button response handler, and then finish loading the view only after it's been called by the alert handler, after the user has responded.
You may or may not have to save extra state information (in extra properties or instance variables instead of method locals) between the first and second methods.
the below can be used, i know its little old question but might be useful for others
..
[alert show];
while ((!alert.hidden) && (alert.superview != nil))
{
[[NSRunLoop currentRunLoop] limitDateForMode:NSDefaultRunLoopMode];
}
I solved it by showing the answer in the "ViewDidLoad", got a delegate to get which button was pressed and then processing the data ONLY if the user pressed "Yes"

What class handles the popup/notification windows on iphone?

I'm looking for the class name of the popup/message windows on the iPhone (it's a blueish window that comes up when you have a missed call, or a message comes in for example.)
The class is called UIAlertView. From the documentation:
Use the UIAlertView class to display an alert message to the user. An alert view functions similar to but differs in appearance from an action sheet (an instance of UIActionSheet).
Use the properties and methods defined in this class to set the title, message, and delegate of an alert view and configure the buttons. You must set a delegate if you add custom buttons. The delegate should conform to the UIAlertViewDelegate protocol. Use the show method to display an alert view once it is configured.
The examples you gave (missed call or incoming text message) are system level alerts that pop up over any application. That functionality is not available through the SDK. lajos's answer does provide the correct way to display an alert, but it is worth remembering you can only do this within your application. You cannot pop up an alert over another app because the SDK currently prohibits an app from running in the background.
Further to this response, UIAlertView is indeed the way to do this and the code you want is:
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Message" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil] autorelease];
[alert show];
Here the alert box will popup with the message "Message" and have a single button titled "OK" which will close the popup when clicked. Check the documentation for other things you can do (more buttons etc).

Bug in AlertView on iPhone?

I write a piece of code to "do something->show alert1->do something->show alert2".
//do something
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Alert 1"
message:nil
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
//do something
UIAlertView *alert2 = [[UIAlertView alloc]
initWithTitle:#"Alert 2"
message:nil
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert2 show];
[alert2 release];
And suddenly a strange thing happened to multiple AlertViews: It shows "Alert 1"->"Alert 2"(Press 'OK')->"Alert 1". Why "Alert 1" shows again? I haven't written any delegate method yet. Maybe a bug?(Thanks to cobbal, alert1 is still there when alert2 appears.)
I find -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex works well. Is the delegate method a common way to show multiple alertViews?
I would guess that alert 1 is shown and then covered by alert 2 since show isn't modal. When alert 2 is closed, alert 1 is still open.
To your second question, alertView:didDismissWithButtonIndex: may work better, but I haven't actually tested that.
The delegate is so that you can be notified when the alert is dismissed, and which button was used to dismiss it. It doesn't impact whether the alert is dismissed at all.
The alert will remain visible until it is dismissed either by you tapping a button (if any - they are not required) or you call either [UIAlertView dismissWithClickedButtonIndex:animated] or the (undocumented) dismiss method of the alert instance.
It looks like (as Cobbal suggested), alert 2 is popping up over alert 1, you dismiss alert 2, and alert 1 remains there (until it itself is dismissed).
Is there a particular reason you want to show a new alert while another is still showing? Perhaps some more context would help us to get to the root of the issue, which I suspect may be a design issue.
[edit] coming back to this and reading again, I wonder if what you are asking about with the delegate method is whether you should be showing alert 2 from there? In which case that's probably what you want - whether directly or indirectly. By indirectly I mean that you may have some state set elsewhere that determines whether alert 2 should be shown (or the circumstances that lead to it). That state (a flag, perhaps) could be set when you show the first alert, and cleared when the alert is dismissed (from the delegate method).
The reason this happens is because UIAlertView doesn't block while it's showing. Any code written after showing an alertview will run straight after the alert is shown.
What you should have is two different methods. One that does something and then shows an alert, and then another that does something and shows another alert.
Kick off the first method to do something and show an alert, and then hook into the alert's delegate method, and when you get the callback from the alertview, run the other method.
This way, the second part of the process won't happen until the user has pressed OK on the alert in first part of the process.