Reputation: 595
For the program that I'm developing, I've created a class Utilities
that contains an alert view with a cancel button. I want to create one NSLog that advises me when the cancel button is pressed.
The method that contains the alert view is called in another class, that is the ViewController.m file, but at the moment no log appears. Can you please help me to understand what I'm doing wrong?
This is the Utilities.h file:
@interface Utilities : UIViewController <UIAlertViewDelegate>
-(BOOL) isOnline;
@end
This is the Utilities.m file:
-(BOOL) isOnline {
Boolean online = false;
Reachability *reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];
NetworkStatus status = [reachability currentReachabilityStatus];
if(status == ReachableViaWiFi) {
NSLog(@"WI FI");
online = true;
} else {
NSLog(@"NO WI FI");
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Warning"
message:@"You are not connected to a wireless network. Please connect your device."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
}
return online;
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex::(NSInteger)buttonIndex
{
if(buttonIndex==alertView.cancelButtonIndex) {
NSLog(@"Clicked the button Ok");
}
}
And here is where I call the method in the ViewController
class:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//Add gradient background
CAGradientLayer *bgLayer = [BackgroundLayer blueGradient];
bgLayer.frame = self.view.bounds;
[self.view.layer insertSublayer:bgLayer atIndex:0];
[self initialize];
Utilities* utilities = [[Utilities alloc] init]; //create an istance of the class Utilities for use their methods in this class
[utilities isOnline]; //call the method isOnline from the class Utilities
}
Update: here there is the error message that now is returning to me Xcode:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString alertView:didDismissWithButtonIndex:]: unrecognized selector sent to instance 0x7558670' *** First throw call stack: (0x1809012 0x162ee7e 0x18944bd 0x17f8bbc 0x17f894e 0xb30e13 0x778d66 0x778f04 0x2027d8 0x1dd4014 0x1dc47d5 0x17afaf5 0x17aef44 0x17aee1b 0x268f7e3 0x268f668 0x73affc 0x2133 0x20c5) libc++abi.dylib: terminate called throwing an exception"
Upvotes: 1
Views: 670
Reputation: 77651
The delegate method you want is...
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
Not sure why but you've changed the method name to buttonPressed:...
.
If you change this back it should work. As long as you've connected the delegate properly.
As mentioned elsewhere, you probably want to use alertView:didDismissWithButtonIndex:
to avoid making UI changes when the alert view is still visible.
EDIT
OK, after further conversations...
Change your interface to...
@interface Utilities : NSObject <UIAlertViewDelegate>
-(BOOL) isOnline;
@end
Change the interface of iPhoneHttpserverViewcontroller
to...
@property (nonatomic, strong) Utilities *utilities;
And change you viewWillAppear to...
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//Add gradient background
CAGradientLayer *bgLayer = [BackgroundLayer blueGradient];
bgLayer.frame = self.view.bounds;
[self.view.layer insertSublayer:bgLayer atIndex:0];
[self initialize];
// I've edited these next two lines to use the property.
self.utilities = [[Utilities alloc] init]; //create an istance of the class Utilities for use their methods in this class
[self.utilities isOnline]; //call the method isOnline from the class Utilities
}
Upvotes: 3
Reputation: 130132
buttonPressed:clickedButtonAtIndex:
is not a delegate method, the correct method is
alertView:clickedButtonAtIndex:
However, usually people use
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
because you don't want to perform another UI actions when the alert is still visible.
Also make sure to use the following for index comparison
if (buttonIndex == alert.cancelButtonIndex) { ...
Edit:
Since the utilities
are used as the delegate, you have to retain them otherwise they will get deallocated
@interface ViewController : ... ()
@property (nonatomic, strong, readwrite) UIUtilities* utilities.
@end
...
self.utilities = [[Utilities alloc] init];
[self.utilities isOnline]
Upvotes: 1
Reputation: 523
Please use Custom Alert View and add button like "Cancel" or "Ok". So you can achieve your goal.For
[button addTarget:self action:@selector(selector:) forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragEnter];
Upvotes: -2