I have UIViewController with UITextView. This TextView always firstResponder.
- (void)viewDidLoad
{
[super viewDidLoad];
[self createBottomBar];
_textView.delegate = self;
[_textView becomeFirstResponder];
}
When user tap "video" button my app show UIAlertView with textfield
- (IBAction)showVideoPicker:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_(#"Link to movie")
message:_(#"Insert a link to YouTube clip or a Coub")
delegate:self
cancelButtonTitle:_(#"Cancel")
otherButtonTitles:_(#"Attach"), nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
When user tap "Attach" button alert dismiss and keyboard dismiss and then immediately show. I would like another scenario: keyboard do not dismiss, because text view should always be a first responder. Just UIAlertView dismiss wihout keyboard dismiss/show animation.
For more understanding see gif here (do not have enough reputation to post images). Sorry for my English.
Have you tried setting the first responder on the Attach button tap via UIAlertView delegate? It's the only way here:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) [_textView becomeFirstResponder];
}
The keyboard might or might not jump a little, but that's all we have.
Related
I had a very strange problem, I want to dismiss the UIAlertView when the user either taps on cancel button or anywhere else. I had read almost all the post and couldn't find an answer that solved my problem do any of you have any idea how to do this? Please guide me through the steps, I will be very thankful for this.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:label.text
message:label1.text
delegate:self
cancelButtonTitle:#"ཕྱིར་འཐེན།"
otherButtonTitles:#"ཉེ་ཆར།",#"དགའ་མོས།",#"ཉེ་ཆར་གཙང་བཟོ།",#"དགའ་མོས་གཙང་བཟོ།",nil];
[alert show];
Try this code
// Add gesture
UITapGestureRecognizer *tapOnView = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(dismissAlert)];
[alert addGestureRecognizer:tapOnView];
Then you can call a function that dismiss your alert
Call touchesEnded method. It'll help you dismiss alertView when user tap anywhere else on the screen
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.yourAlert dismissWithClickedButtonIndex:0 animated:YES];
}
and remember to create yourAlert as strong reference
I'm hoping someone can help me figure this out...I'm a beginner Xcode / Objective-C programmer. I'm working on a app that is a continuation of last semester.
1: So I created a button and I need it to execute this 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];
}
How/where do I put this function? Is it in the button properties? Or would I write this in a custom class/controller and link it to it?
2: How do I get it to listen for the alert to return on: - alertView:didDismissWithButtonIndex:
3: From there, how would 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];
}
}
You will need a custom UIViewController to house the logic for your button and alert view interactions. I am assuming you know how to do that.
Once done, assuming you have a reference to your button property in your view controller, you can programatically add a target to your button and pass the selector cancelTapped as a parameter:
[myButton addTarget:self action:#selector(cancelTapped) forControlEvents:UIControlEventTouchUpInside];
Alternatively you could control-drag from the button in your Storyboard to the header file of your custom UIViewController, and define an IBAction. That will create an empty cancelTapped method in your implementation which you could then add your logic in.
As for listening on UIAlertView messages, you will need to make your custom UIViewController a delegate of the UIAlertView by passing "self" as the delegate in the following statement:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Notification" message:#"My Message" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes"];
Your CustomViewController should also be declared as a UIAlertViewDelegate.
CustomViewController.h
#interface CustomViewController : UIViewController<UIAlertViewDelegate>
#end
Hope this helps!
By the using VIewWillDisappear method to detect the press of The back button of NavigationItem:
-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
// Navigation button was pressed. Do some stuff
[self cancelTapped];
}
[super viewWillDisappear:animated];
}
- (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];
}
For More info && also Custom UIBArButtonItem Check Here
This question already has an answer here:
UIAlertView not functioning correctly
(1 answer)
Closed 8 years ago.
I am trying to make a popup appear when the user clicks a button with a warning, and if they click cancel, it will be dismissed, but if they click continue, I want it to present the view controller. Here is my code, but it only dismisses the popup no matter which button I press:
- (IBAction)latest:(id)sender {
alert = [[UIAlertView alloc] initWithTitle:#"WARNING" message:#"Continuing will use internet and may cause app to slow down in large crowds" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:#"Continue", nil];
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 2) {
UIViewController *NVC = [self.storyboard instantiateViewControllerWithIdentifier:#"Latest"];
[self presentViewController:NVC animated:YES completion:Nil];
}
}
Your problem is simple - you need to pass self as the alert view's delegate, not nil.
Also, in your delegate method, don't hard code button indexes. Do this instead:
if (buttonIndex == alertView.firstOtherButtonIndex) {
}
You only have 2 options to click. Indexes start at 0 not 1 so change if (buttonIndex == 2) to if (buttonIndex == 1)
Ok I figured it out. I had to set the delegate to self, it was set to "nil".
I've been searching for a few hours and haven't found any answers, so I would really appreciate your help!
I am designing an app and need to segue to a different view controller when a button in an alert is pressed. I already have the alert set up, but just need the code to segue to a new view.
Try this. create alertview first.
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Message" message:#"Open New controller?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes",nil];
[alertView show];
[alertView release];
Implement the AlertView Delegate Method
#pragma mark AlertView Delegate
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != alertView.cancelButtonIndex)
{
Viewcontroller *vc = [[UIViewcontroller alloc] initWithNib:#"Viewcontroller"];
[self presentModalViewController:vc];
[vc release];
}
}
Implement the UIAlertViewDelegate and add the method alertView:clickedButtonAtIndex:. Once the right button is clicked, call the method to segue into the new view.
Implement UIAlertViewDelegate and then you get a callback with which button was pressed when the alert was dismissed. Simply call performSegueWithIdentifier in there.
More details at the UIAlertViewDelegate protocol reference
In my UIAlertView, I want to open another UIView when "OK" button is pressed.
But the problem is, even after the UIView is displayed, alert remains in screen and once it fades away, the UIView seems to be disabled.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Add details" message:#" Do you like to set the details now?" delegate:self cancelButtonTitle:#"Yes" otherButtonTitles:#"No",nil];
[alert show];
[alert release];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{ // the user clicked one of the OK/Cancel buttons
NSString *title = [alertView title];
if([title isEqualToString:#"Add details"])
{
.......
Any help would be appreciated!
Why not check for the button pressed in the delegate method instead of the title?
That is, in
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0){
// User pressed "YES"
}else{
// User pressed "NO"
}
}
The cancel button has index 0 and the other buttons increase in index. In order to detect which alert was this, you should also give it a tag. Hope this helps.
Could be because the new view is added before the alert view is actually dismissed. So better use the didDismissWithButtonIndex delegate to show a new view on button click event of an existing alert view, instead of clickedButtonAtIndex
- (void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
//Add the view
}
Instead of
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//Add the view
}