Adding subview on UIAlertView for iOS7 - uialertview

In iOS 7 the [alert addSubview]; method is not working for adding any subview on the alert view.
So I used [alert setValue:view forKey:#"accessoryView"]; to display the UIActivityIndicator on the alert view.
My questions are:
Is #"accessoryView" public to the API to add the view on alert ?
Will apple accept this type of behaviour ?

You SHOULD NOT be adding views to a UIAlertView this is a one fast track ticket to getting your app rejected from the App Store, check out the section mark as Subclassing Notes from the Apple UIAlertView Documentation
Subclassing Notes
The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.
In iOS7 Apple basically re-enforced the above by making it so addSubview: actually did nothing, it is there still though because UIAlertView is a subclass of UIView and UIView can still respond to this method. Many have however written custom AlertViews check out iOS Custom AlertView for example

Related

Dismissing an Alert View in different method than what it is in

I have an in app purchase in my app and when the purchase is "purchasing", or in progress, I have an alert view come up that says "Loading...". When the purchase was successful, restored, or failed, I'd like to call a method that releases the alert view. The only problem is, I can't try to release the alert view in another method because it will have no idea what alert view I am talking about and will produce an error. Now I have no idea if this is even the best way of trying to accomplish this, so all ideas are appreciated. Thanks!
case stateIsPurchasing { //or whatever it's called
UIAlertView *alert = [[UIAlertView alloc] message and delegate and button stuff here];
[alert show];
[alert release];
}
The UIAlertView is definitely not the right control to do this.
You should use the UIProgressView if you are able to display finite progression or UIActivityIndicatorView to show the "spinner".
I wouldn't use an alert view for this. Look for a progress HUD, such as SVProgressHUD, an excellent and beautiful loading view.
http://cocoacontrols.com/platforms/ios/controls/svprogresshud
SVProgressHUD functions as a singleton, so you can show/stop it from any class.
Declare a UIAlertView in your header as a retained property, synthesized, and released in dealloc. Store the alert view that you create in that code snippet using this pointer, and use the declared pointer in your other method. But don't call [alert release]; when you create the alert view (unless you like EXC_BAD_ACCESS errors!). Oh, if you're adding in-app purchases, watch out for Lodsys...

Custom UIAlertView with Custom Buttons

I need to show a Custom UIAlertView which is going to have an image as a background and two Custom Buttons which will be not the regular UIAlertView Buttons. These buttons will be customized as well and would have their own Artwork.
The example above has a background but I also want to add Custom Button on it.
I am following this guide but I don't think it address the Custom Button handling.
How to do that? Any ideas?
Apple doesn't appear to like you overly-customising UIAlertView, and I've heard of a number of occasions where they've declined an app going into the app store because of it.
Because of the extent of customisation you're after, I suggest you create your own new Alert class that animates in and has a background shadow etc with buttons that you can customise the location/look of.
I found this blog post by Jeff LaMarche to be really helpful in making custom alert views: http://iphonedevelopment.blogspot.com/2010/05/custom-alert-views.html. He goes through the steps of making a custom class since modifying UIAlertView can cause App Store rejection (however he warns that using custom techniques can still cause HIG violations, but I doubt they will for what you're trying to do). By the time you're done, you'll have a custom alert view class that can be added in much the same style as UIAlertView:
At this point, we're done. We can now
use this custom alert view exactly the
same way we use UIAlertView:
CustomAlertView *alert = [[CustomAlertView alloc] init];
alert.delegate = self;
[alert show];
[alert release];
He creates a custom text input view. Obviously, in your case, you would want to use a different background and instead of adding a text field you'd stick to just the buttons. Since he makes custom buttons in his view too it should cover all your needs, if not more.
Unfortunately Apple does not allow subclassing UIAlertView:
The UIAlertView class is intended to be used as-is and does not
support subclassing. The view hierarchy for this class is private and
must not be modified.
The easiest way would be to create your own class with similar behavior.
Here's an example:
http://iosdevtricks.blogspot.com/2013/04/creating-custom-alert-view-for-iphone.html

UIALertView customization

I am developing an iPhone application, in which I want to use customized alert sheet. The customization is required since I want to set image for Alert-Sheet buttons, change the size of these button etc.
I have done the following things:
Created UIView with customized controls that I wanted.
Created and displayed UIAlertView
In the delegate method of UIAlertView (UIAlertViewDelegate) i.e
- (void)willPresentAlertView:(UIAlertView *)alertView
I am removing all the subviews of UIAlertView and adding my customized view as subview.
Everything works fine till here. Since I have used customized buttons, I need to remove the alert sheet explicitly in the Action Method of the button by calling dismissWithClickedButtonIndex:animated: on UIAlertView.
Even though the UIALertView gets dismissed, it takes around 0.5 second to get dismissed.
Can someone help me out to solve the problem of this delay in dismissing OR some other way of customization of Alert View buttons.
Thanks and Regards,
Deepa
I could get it worked by passing YES to dismissWithClickedButtonIndex:animated: call i.e [alertView dismissWithClickedButtonIndex: 0 animated: YES]. Initially I was passing the flag as NO. But, I don't know whey it takes less time if we pass the animation flag as YES.
Anyone knows this?
Instead of doing this
/*
1. Created UIView with customized controls that I wanted.
2. Created and displayed UIAlertView
3. In the delegate method of UIAlertView (UIAlertViewDelegate)
*/
do this:
Create a class like this:
#interface CustomAlertView : UIAlertView
{
//For ex:
UIButton *myCustomButton;
//and other custom controls
}
Implement it in following method:
-(id)init
{
}
In the above method use:
[self addSubView: myCustomButton];
I have given just the idea. I have code but not presently to share with you.
If you are not able to implement the above I will provide later.
Thanks.
Here is the library which can solve your UIAlertView Customisation issue. It can also work as UIActionSheet. It has very good customisation options.
https://github.com/Codigami/CFAlertViewController

Why releasing an UIAlertView just afer showing it whereas it is not a blocking method?

I have been studying how to display a modal view with UIAlertView for a few hours and I understood that showing it does not "block" the code (the modal window is displayed and the program keeps running - we must use delegate to catch the selected actions on this modal window). Then I studied several examples and noticed that every example always release the modal window just after showing it. How can this work properly since the view will be released instantly as the code does not stop ?
Here is the example (there are many others on Google):
[[UIAlertView alloc] initWithTitle:#"Title" message:#"Message..." delegate:NULL cancelButtonTitle:#"OK" otherButtonTitles:NULL];
[alert showModal];
[alert release];
Thanks for your help,
Apple 92
I'm not sure where you're getting -showModal from (the usual method is just -show), but that act adds the alert to the view hierarchy. When a view is added as a subview of another view (I believe in this case it's a system-level view that is being added to) it's retained automatically, so you don't have to.
The alloc method will return you an instance that has a retain count of 1.
The showModal method probably retains the alert view so it remains on screen (and retained) until a button is tapped. It makes sense to me, since you are presenting it as a modal window, so it doesn't have a "parent", that is responsible of releasing it.

How do I code a green button in UIActionSheet?

I am using the code:
{
randomstatus=0;
msg=[[NSString alloc]initWithFormat:#"Good job, do you want to continue?"];
UIActionSheet *actionSheet=[[UIActionSheet alloc]initWithTitle:msg delegate:self cancelButtonTitle:#"No" destructiveButtonTitle:#"Yes" otherButtonTitles:nil];
[actionSheet showInView:self.view];
[actionSheet release];
[msg release];
}
I don't want to change the code, but I need the "destructiveButton" to be green instead of red. Is this possible, or do i need to use a different button?
There is no "standard" way of changing the appearince of the buttons. Any ways you use will essentially be hacks and may break in the future if Apple change the UIActionSheet component. They may also get your app rejected if they upset the App-Store gods.
I think the most future-proof way of acheiving this is to create your own action sheet class from scratch, ie not subclassing UIActionSheet (as this may break in the future). Although this may be a bit more work up-front than some hack, the extra flexibility you'll gain will come in useful in the future.
This shouldn't be too difficult. You'll need a view which is the background for the action sheet, which you can get by taking a screenshot of a standard UIActionSheet and some photoshopping. Then add your custom buttons as sub-views. A bit of animation for bringing up the view and you're done.
I would aim to have your class implement all the methods the UIActionSheet does, as well as firing off the methods UIActionSHeetDelegate expects. This way you'll be able to substitue it in anywhere you'd otherwise use a native UIActionSheet
Unfortunately there aren't any officially provided methods for customizing UIActionSheet buttons.
However you can access the subviews of the UIActionSheet (which could break in a future iPhone OS update), or add a new view with a button that covers the original destructive action button (once again, this may break).
While not directly related to changing the color of a button on a UIActionSheet, this previous question: iPhone Disabling UIActionSheet buttons demonstrates a few ways you could add custom views to a UIActionSheet.