I got a button that leads to a tableViewController.
I created UIAlertView that gets triggered when the resultArray is null, which also means there is nothing to show on tableView but when user clicks ok, nothing changes and it goes to empty tableView.
How can I stop going into the other view controller?
this is my code
if (refilteredArray.count==0) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"No result"
message:#"Please enter a different combination."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
The segue is not triggered with code, its a button triggered IBAction
- (IBAction)motorButton:(id)sender {
...
}
you can use following method
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
in the method body you can verify if your array is empty or not and if it is return NO. To be more specyfic you should check your segue identifier (remember to add it to your segue in IB first)
You need to go for if.. else block
if (refilteredArray.count==0) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"No result"
message:#"Please enter a different combination."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}else{
[self performSegueWithIdentifier:#"yourSegue" sender:sender];
}
For more info, check this link about segue
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 created login page with username and password.when i click login button, wrong password message will appear.But it goes to next page.can anyone help me? my code is below:
-(IBAction)enterCredentials
{
if ([[credtialsDictionary objectForKey:usernameField.text]isEqualToString:passwordField.text])
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"correct password" message:#"This password is correct." delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil,nil];
[alert show];
}
else
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Incorrect password" message:#"This password is incorrect." delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil,nil];
[alert show];
}
}
You need to implement
- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender
method to cancel or allow segue to happen.
It looks like you have create segue link for UIButton tap event to next UIViewController in Storyboard, So what ever you do it will push to next controller.
Go to Storybord
Remove that segue from Storyboard and do push navigation from code.
Select next UIViewController and give unique storyboard id like below reference image.
Go to controller class from where -(IBAction) method is calling.
In that method create an instance of next view controller.
Update code with following:
-(IBAction)enterCredentials
{
if ([[credtialsDictionary objectForKey:usernameField.text]isEqualToString:passwordField.text])
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"correct password" message:#"This password is correct." delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil,nil];
[alert show];
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#“ViewController”];
[self.navigationController pushViewController:controller animated:YES];
}
else
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Incorrect password" message:#"This password is incorrect." delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil,nil];
[alert show];
}
}
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
So I'm making a photo editing app for school from last semesters project. When a user clicks the back button (in editor mode) which in my case is called "Product Selection" I'd like to have a pop-up come up and say "Do you want to delete everything and go back to product selection?" to see if they want to discard their work, and if the user selects yes, the entire project is discarded and they are put back in the lobby.
Where do I put this code to do this? I found the "Product Selection" button in the min story board but not sure what to do from there.
The code for the pop-up I was going to use is:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Notification"
message:#"Do you want to delete everything and go back to product selection?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes"];
[alert show];
[alert release]
Any help/wisdom would be extremely appreciated!
I would create a "Cancel" UIBarButton which executes a custom function:
- (void)cancelTapped {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Notification" message:#"Do you want to delete everything and go back to product selection?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes"];
[alert setTag:1];
[alert show];
}
And listens for the alert to return on:
- alertView:didDismissWithButtonIndex:
From there, I would write the logic to hide the page and pop the view.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (alertView.tag == 1 && buttonIndex == 1) {
// Delete data and return to lobby
[self.navigationController popViewControllerAnimated:YES];
}
}
Use this delegate method (don't forget adding UIAlertViewDelegate on your .h):
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Notification" message:#"Do you want to delete everything and go back to product selection?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes"];
alert.cancelButtonIndex = 0;
[alert show];
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
// If user confirmed:
if (buttonIndex != 0) {
// Do what you need.
}
}