UIActionSheet Delegate Self Warning - ios

Since the introduction of iOS 7, a lot of improvements and changes were made to Objective-C. I've created a UIActionSheet below and assigned the delegate to self so that I could program each button press to complete an action.
- (IBAction)sortButton
{
UIActionSheet *sortSheet = [[UIActionSheet alloc]
initWithTitle:#"Sort By"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Featured"
otherButtonTitles:#"Price", #"Brand", nil];
[sortSheet showInView:self.view];
}
This is the code I use to check against what the user did when they clicked on a specific button in the UIAlertView:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0)
{
UIAlertView *dueDateAlert = [[UIAlertView alloc]
initWithTitle:#"Featured"
message:#"This button formats the list for items based on Feature."
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:#"Ok", nil];
[dueDateAlert show];
}
else if(buttonIndex == 1)
{
UIAlertView *creationDateAlert = [[UIAlertView alloc]
initWithTitle:#"Price"
message:#"This button formats the list for items based on Price."
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:#"Ok", nil];
[creationDateAlert show];
}
else if(buttonIndex == 2)
{
UIAlertView *subjectAlert = [[UIAlertView alloc]
initWithTitle:#"Brand"
message:#"This button formats the list for items based on Brand."
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:#"Ok", nil];
[subjectAlert show];
}
}
When I run this code, I get the following Warning: Sending SecondViewController *const_strong to parameter of incomplete type 'id<UIActionSheetDelegate> on the self text in the creation of the UIActionSheet. I'm assuming it's saying that I'm doing something "inefficiently", and I was wondering what needs to change in order for this warning not to appear.

It seems that your class does not confirm to UIActionSheetDelegate protocol.
Check that you have something like
#interface YourClass () <UIActionSheetDelegate>

Related

Reduce alert view coding

All.
In my iOS app.
On many pages I am having Too many alerts and also with many Network conditions.
With too many alert texts I am fed up.
And Every time I have to put the same code.
Can I declare this Code in Some Helper Class ?
Or Reuse this Code ?
-(BOOL)checkInternetAndlocationServices {
if(IS_INTERNET) {
if([CLLocationManager locationServicesEnabled] &&
[CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied){
return YES;
}
else
{
NSLog(#"Location services are disabled.");
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Location services are off." message:#"This app requires an Location services." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"Location Services", nil];
[alert setTag:NO_LOCATIONSERVICES];
[alert show];
return NO;
}
}
else
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Internet connection is off." message:#"This app requires an internet connection and locations services, please enable internet connection and location services." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"Settings", nil];
[alert setTag:NO_INTERNET];
[alert show];
return NO;
}
}
Thanks.
Please edit this question, if you found it useful..
Thanks for giving good approaches, still any other ways, examples are most welcome.
You could make a helper class and use class methods to show alert.
You could also make a UIAlertView category and make a class method for showing alert.(Edit)
#implementation UIAlertView (MyAlert)
+(void) showAlertWithTitle:(NSString *)title message:(NSString *)message {
[[[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil] show];
}
#end
You could define a macro in .pch file or some helper header file for showing alert.#define Alert(title,msg,target) [[[UIAlertView alloc] initWithTitle:title message:msg delegate:target cancelButtonTitle:#"OK" otherButtonTitles:nil, nil] show]
Alert(#"This is Title",#"This is message",self);
You can make an NSObject class and write method whether Instance or Class method like this and pass only the message and the delegate whether needs to set nil or self like this:-
+(void)showAlertViewWithAlertMessage:(NSString*)alertMessage withDelegate:(id)delegate
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Title" message:alertMessage delegate:delegate cancelButtonTitle:OK_TAP otherButtonTitles: nil];
[alert show];
}
You can use it like this:-
[Classname showAlertViewWithAlertMessage:#"your message" withDelegate:nil];

iOS UIAlertView being shown more than once

Hi I have used the following function to call an alert view with a specific message. The NSLogs are being printed only once but alert view is called more than once.
I am using UITabBarController for any additional info.
- (void)showUIAlertWithMessage:(NSString *)message {
NSLog(#"showUIAlertWithMessage called");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Something went wrong!😕😕"
message:message
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
}

Change iOS tab view from alert

I have a tabbed application with 2 tab views. I want to change the view from the second tab to the first when the user presses "OK" on an alert view.
The following code snippet works when I use it on an -IBAction button pressed, but it doesn't do anything when I use it inside the alert code:
self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
Why does it not work when used as below, in my SeondViewController.m in my
- (void)viewDidLoad {
[super viewDidLoad];?
UIAlertView *alert =[[UIAlertView alloc]
initWithTitle:#"Camera and Target coincident"
message:msg
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
I'm just learning this, so anything you can offer would be helpful.
When I use the following code:
if ([ theProjection Trans_Initialise] == 1) {
NSString * msg = nil;
UIAlertView *alertView =[[UIAlertView alloc] initWithTitle:#"Camera and Target coincident" message:msg delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:#"OK"]) {
self.tabBarController.selectedIndex = 0;
}
}
I get the error message on -(void) alertView "Invalid argument type void to unary expression" Am I doing something grammatical, or is it (quite possibly) something I just didn't understand?
First of all, you need a delegate call back from the alert view informing you about the button that was clicked.
Your alert view should be created like this :
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:#"Camera and Target coincident" message:msg delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
Then in your m file implement the function :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:#"OK"]) {
self.tabBarController.selectedIndex = 0;
}
}

Order of executing methods

I have a problem about order of executing methods.
if (indexPath.row == 2) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:#"Data will be downloaded"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
if([app getData]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:#"Data is downloaded."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
When I run this code snippet, I want to first show an alert view. However, it calls the getData method before showing alert view. Once the getData method is completed, alertView comes to the window.
How can I correct this?
The function is called asynchronously therefore appData gets called before the first alert view is visible. Change your code to this:
if (indexPath.row == 2) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:#"Data will be downloaded"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
The below method will be called when the user presses OK on your first alert, but it means that while the first alert is visible, your code for appData would not start.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if([app getData]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:#"Data is downloaded."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
Note: Remember to add UIAlertViewDelegate in your view controller
I don't have Xcode with me, but I'll take a stab at it anyway. Your problem is because the alert won't show until the main loop iterates. And it won't iterate until your getData method is executed since you're doing this on the main thread. So you need to fork.
Try wrapping your if() in something like this:
dispatch_async(dispatch_get_main_queue(),
^ {
if([app getData]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:#"Data is downloaded."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
});
It's because the run loop must continue running so [UIAlertView show] is asynchronous and will not block the current thread. If the main thread was blocked then no user interface events could be delivered.
If you want something to occur after the first alert view is dismissed then do it within the alertView:clickedButtonAtIndex: delegate method.

is possible to remove previous alert view?

i have create two UIAlertView views in one method. Code like below
-(void) alert{
UIAlertView *alert_1 = [[UIAlertView alloc] initWithTitle:#"Message" message:#“Alert 1” delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert_1 show];
UIAlertView *alert_2 = [[UIAlertView alloc] initWithTitle:#"Message" message:#“Alert 2” delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert_2 show];
}
after call this method. iPhone app will appear popup for 2 times.
first appear is alert_1, disappear alert_1 and appear alert_2
after user press ok button in alert_2 thn appear alert_1
should be remove alert_1 when appear alert_2
is possible to remove previous alert view?
Send message - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated to alert1.
UIAlertView *alert_1 = [[UIAlertView alloc] initWithTitle:#"Message" message:#“Alert 1” delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert_1 show];
[alert_1 dismissWithClickedButtonIndex:0 animated:YES];
UIAlertView *alert_2 = [[UIAlertView alloc] initWithTitle:#"Message" message:#“Alert 2” delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert_2 show];
I am not clear about what your requirement is. But from what I understood you want alert_2 to popup first and when clicked on the "OK" button you want to dismiss that alert view and popup alert_1
- (void) alertview
{
alert_1 = [[UIAlertView alloc] initWithTitle:#"Alert 1" message:#"Message" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert_1 setTag:1];
alert_1.delegate = self;
alert_2 = [[UIAlertView alloc] initWithTitle:#"Alert 2" message:#"Message" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert_2 setTag:2];
alert_2.delegate = self;
[alert_2 show];
}
- (void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView.tag ==2)
{
[alert_1 show];
}
}
Please note to declare your alert views in your .h file
Get UIAlertView by its tag or #Property and use this [myAlertView dismissWithClickedButtonIndex:-1 animated:YES];

Resources