I have ViewController with UITableView. if i touch on any Cell, it shows information about this Cell in another view. It is working well. But when i add MyViewController's view to UIAlerView's view, if i touch on AlertView, it is giving runtime error.
this is my code:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
MyViewController *VC = [[MyViewController alloc] init];
[alert setValue:VC.view forKey:#"accessoryView"];
[alert show];
any help please ...
my program shows like this:
An alert view is simply shown on the screen. It doesn't matter which view controller is currently visible, when the show method is called, the alert view is added as the top view.
The only thing that would matter in terms of view controllers is which one you want as the delegate view controller, which can be assigned to objects other than self.
Refactor your code to look like this:
MyViewController *VC = [[MyViewController alloc] init];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"" delegate:VC cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
Notice how the VC you want to display is now the delegate to the alert?
Related
I've successfully integrated Chartboost rewarded video, it works but i want to implement the code that will increase credits when a user has completely watched a rewarded video. I've tried to implement in app delegate and it works calling it but due to some internal functions i will need to implement in specific view controller. so when i put the same code
-(void)didCompleteRewardedVideo:(CBLocation)location
withReward:(int)reward {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Really reset?" message:#"hi ?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
// optional - add more buttons:
[alert addButtonWithTitle:#"Yes"];
[alert show];
}
the alert view will not appear if i implement this code in specific view controller instead of appdelegate. i assume it is not called in view controller since it has no response. i need help to implement in view controller
In order to make UI changes, you should call it on main queue.
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Really reset?" message:#"hi ?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
// optional - add more buttons:
[alert addButtonWithTitle:#"Yes"];
[alert show];
});
Or you can just pass the delegate of the view and show the alert on it.
-(void) DisplayMessage:(NSString*)message withDelegate:(id)delegate
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Title"
message:message delegate:delegate
cancelButtonTitle:nil
otherButtonTitles:#"Ok", nil];
[alert show];
}
I have to fix some bugs in an old application and got a really weird one. All alert views are shown in the top left corner for some reason. App doesn't use storyboards, main window is loaded from xib. What can cause this?
UPDATE: Code to initialize alert view is pretty standard. I'm calling it inside one of the controllers of UITabBarController. Same bug occurs even if I leave app's main window empty and initialize alert view in application:didFinishLaunchingWithOptions:
UIAlertView *testAlert = [[UIAlertView alloc] initWithTitle:#"Title"
message:#"Message"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil, nil];
[testAlert show];
I fixed this by simply replacing the [alert show] with the present [self presentViewController:alert animated:YES completion:nil];
I have a method called errorHandle which gives the alert view and I am calling this method in the #try block
-(void) errorHandle {
UIAlertView *disp = [[UIAlertView alloc] initWithTitle:#"Invalid QR Code" message:nil delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[disp show];
//[disp dismissWithClickedButtonIndex:0 animated:YES];
}
#try{
dispatch_async(dispatch_get_main_queue(), ^{
[self errorHandle];
});
}
Here I am getting the alert view but when I tap on the cancel button,the alert view is dismissing and appearing back again . I could not dismiss the alert view with single tap but after tapping several times the alert view is dismissed but the CPU usage during the operation boosted to 190%.
After the alert view is dismissed ,I again tried to get the alert view again I have to tap the cancel button several times. Is there anyway to dismiss the alert view properly on main thread and make it not to appear again?
I tried the following code but it didn't work out for me
1)
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:#"Event" message:#"Event added in calendar" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil];
[alertview performSelectorOnMainThread:#selector(show) withObject:nil waitUntilDone:YES];
2)
__block UIAlertView *alert1 =[[UIAlertView alloc] initWithTitle:#"Event" message:#"Event added in calendar" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
dispatch_async(dispatch_get_main_queue(), ^(void){
[alert1 show];
});
Can we generate alert view in #finally block
is this possible?
#finally{
// alert view code
}
Can anyone answer me about these two issues?
I'm working on a losing screen and I'd like to let users view their game before showing the stats screen. I've decided to use an alert to show users the current screen. After they hit OK, they should be taken to the stats screen.
The problem is, the stat screen pops up at the same time as the alert. How can I make sure the stats screen opens after the user closes the alert?
if ([self.model userHasLost]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Sorry!" message:#"You lost..." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[self _showGameEndScreenWitnWin:NO]; //Stats screen should open after alert is closed
}
You need to use delegate method of UIAlertView and also need to set delegate of UIAlertView = self,
Following is delegate method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
check buttonIndex in this method it's give you NSInteger and manage click of button.
For more information read this UIAlertView Delegates.
You need to set the alert view delegate. In this example self will conform to the UIAlertViewDelegate protocol.
if ([self.model userHasLost]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Sorry!" message:#"You lost..." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
alert.delegate = self;
[alert show];
}
You can then listen to a delegate method for when the alert view is dismissed :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
[self _showGameEndScreenWitnWin:NO]; //Stats screen should open after alert is closed
}
For this situation need to use delegate method,
For simple Example Refer Here: http://www.idev101.com/code/User_Interface/UIAlertView.html
I have a UIActionSheet to provide user options on a table view controller. I have implemented this before without issue, but for some reason now, my event handler does not fire when a button is clicked. In my header file, I have implemented the proper delegate as follows:
#interface GaugeDetailTableViewController : UITableViewController <UIActionSheetDelegate>
In my implementation, I have the following code to support the action sheet:
-(void)loadPopup{
UIActionSheet *popup = [[UIActionSheet alloc]
initWithTitle:#"Options"
delegate:nil
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Add to Favorites", #"Gauge Detail Help", #"Main Menu", nil];
popup.actionSheetStyle = UIActionSheetStyleDefault;
[popup showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(#"%d\n", buttonIndex);
}
The action sheet appears properly on the view controller, but for some reason, the action sheet click event handler is never reached. Any suggestions? Thanks!
You need to set the delegate:
UIActionSheet *popup = [[UIActionSheet alloc]
initWithTitle:#"Options"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Add to Favorites", #"Gauge Detail Help", #"Main Menu", nil];
popup.actionSheetStyle = UIActionSheetStyleDefault;
It looks like you have forgotten to set the delegate. You need to implement UIActionSheetDelegate (which it looks like you have) and then set the delegate!