I have alert-view that has a tableview as a subview. I want to disable firstOtherbutton after tableview didSelectRowAtIndexPath. I can call alertViewShouldEnableFirstOtherButton but firstotherbutton doesn't change.
- (void)viewDidLoad
{
self.announcement=[[UIAlertView alloc] initWithTitle:#"Announcement" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:#"Tamam", nil];
[announcement setValue:self.tbl.view forKey:#"accessoryView"];
announcement.delegate =self;
announcement.tag=100;
[announcement show];
[self.tbl.tableView reloadData];
}
-(void)didSelectAnswer
{
[self performSelector:#selector(alertViewShouldEnableFirstOtherButton:) withObject:announcement afterDelay:0.001];
}
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
if (alertView.tag==100)
{
if(!self.tbl.deger)
{
return YES;
}
return NO;
}
return NO;
}
you have to write your custom alertview to achieve this feature.
Here are some open source implementations of alertview for iOS:
https://www.cocoacontrols.com/tags/uialertview
Related
I've read the related posts but must be missing something since my ClickedButtonAtIndex method is not being called after hitting the OK button on my alert. I set a breakpoint within the ClickedButtonAtIndex method, it is never reached and the NSLog is never executed.
Here's the .h code:
#import <UIKit/UIKit.h>
#interface MainPage : UIViewController <UIAlertViewDelegate> {
IBOutlet UILabel *sendtxtlabel;
}
- (IBAction)button:(id)sender;
#end
Here is the .m code here:
#interface MainPage ()
#end
#implementation MainPage
- (void)viewDidLoad {
[super viewDidLoad];
sendtxtlabel.text= [NSString stringWithFormat:#"Number 800-555-1212"];
// Do any additional setup after loading the view.
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Vehicle Owner"
message:#"Enter Phone Number"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"ok", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
alert.tag =12;
//[alert addButtonWithTitle:#"ok"];
[alert show];
NSLog(#"ok");
}
- (void) alertView: (UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// Capture the phone number input from the alert pop-up window. UIAlertView Delegate added to allow the OS trigger this method to read the data.
if (alertView.tag == 12) {
if (buttonIndex == 1) {
UITextField *textfield = [alertView textFieldAtIndex:0];
NSLog(#"phonenumber: %#", textfield.text);
}
}
}
I have an alertView declared in a method like this.
UIAlertView* alertView =[[UIAlertView alloc]initWithTitle:#"Check In Alert" message:#"Are you sure he/she is there?" delegate:self cancelButtonTitle:#"Yes" otherButtonTitles:#"No", nil];
alertView.tag = sender;
[alertView show];
So in alertView method of clickedButtonIndex I have this code.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if([alertView.title isEqualToString:#"Check In Alert"])
{
ClassRoomParticipant *clsrp;
NSMutableArray *data = nil;
data=[self.presentDataArray objectAtIndex:0];
clsrp = (ClassRoomParticipant *)[data objectAtIndex:alertView.tag];
NSLog(#"Do Nothing");
if (buttonIndex == 0)
{
[clsrp setGetParcpntCheckInStatus : 2];// Checked in by Teacher
}
else if (buttonIndex == 1)
{
[clsrp setGetParcpntCheckInStatus : 3];// Not checked in by Teacher, still Absent
}
return;
}
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"OK"])
{
[self.navigationController popViewControllerAnimated:YES];
}
else if([title isEqualToString:#"Button 2"])
{
//NSLog(#"Button 2 was selected.");
}
else if([title isEqualToString:#"Button 3"])
{
//NSLog(#"Button 3 was selected.");
}
}
I have a method like this.
-(void)changeStudentAction:(id)sender
{
CustomUIButton *button=sender;
NSInteger flag = 0;
if (button.selected)
{
[button setSelected:NO];
[button setBackgroundImage:[UIImage imageNamed:#"Absent_on_coloured.png"] forState:UIControlStateNormal];
flag=0;
}
else
{
[button setSelected:YES];
[button setBackgroundImage:[UIImage imageNamed:#"Present_on_coloured.png"] forState:UIControlStateNormal];
flag=1;
}
if (button.collectionIdentifier == ClsRmPrctPresntCollectionView)
{
[self ModifyDataArray:button.taggy flag:flag identifier:ClsRmPrctPresntCollectionView change:#"Button" toDate:nil section:button.section];
[self modifyCollectionArray:PRESENT processingData:self.presentDataArray identifier:ClsRmPrctPresntCollectionView index:button.taggy section:button.section];
}
self.isAnyChangesHappened=YES;
}
I want to call this changeStudentAction method inside of alertView button click "Yes".When I am calling this method inside buttonIndex==0, it gives me error. Can you please tell me how to call this method inside the alertview.
1) Create an outlet of the "CustomUIButton" for which "changeStudentAction" is action
2) in alert view delegate call [self changeStudentAction:self.customUIbuttonOutlet]
I guess it is showing you error because of alertView.tag = sender; statement. Make sure sender is integer.
Also you did not set delegate of alertView. Try this : alertView.delegate = self;
Rest all is seems to be perfect. Just call changeStudentAction method in the correct block of - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex method.
I want to use push segue to change view. Also, I want to past few data to destination view. Before the segue change view, I want to pop a alertview with textfield to check user password. If match, it will change the view. If not, it will stop the push segue.
I know that if want to prevent the push segue to do something first need to use the below method, but it cannot get the segue.destinationViewController. Is anyway can replace the segue.destinationViewController?
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
Moreover, can I get the alertView textfield result in the - (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender method?
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
if ([identifier isEqualToString:#"chatSegue"]) {
NSLog(#"should");
NSString *plock = [Server getUserDatawithkey:#"plock"];
if ([plock isEqual:#"1"]) {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Privacy Lock" message:#"Please enter your password:" delegate:self cancelButtonTitle:#"Continue" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * addFriendField = [alert textFieldAtIndex:0];
addFriendField.keyboardType = UIKeyboardTypeDefault;
addFriendField.placeholder = #"Enter your password";
alert.tag = ALERT_TAG_PW;
[alert show];
// Can I get the alertView textfield result at here?
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
friendCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
// Is anyway to replace the segue.destinationViewController?
chatViewController *cvc = segue.destinationViewController;
cvc.fid = cell.fid.text;
cvc.title = cell.f_name.text;
return YES;
}
return NO;
}
return YES;
}
Is somebody can help me? Thanks!
Instead of creating the alert in shouldPerformSegueWithIdentifier you should create it somewhere else (whatever is calling your push) and depending on the resulting action of that alert perform [self performSegueWithIdentifier:#"yourIdentifier" sender:self];
To handle the result of your alert you want to use the UIAlertViewDelegate and the method - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex.
Might look something like this...
- (void)getServerStuff {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Privacy Lock" message:#"Please enter your password:" delegate:self cancelButtonTitle:#"Continue" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * addFriendField = [alert textFieldAtIndex:0];
addFriendField.keyboardType = UIKeyboardTypeDefault;
addFriendField.placeholder = #"Enter your password";
alert.tag = ALERT_TAG_PW;
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(alertView.tag == ALERT_TAG_PW) {
if(buttonIndex == 0) {
[self performSegueWithIdentifier:#"pushViewIdentifier" sender:self];
}
else if (buttonIndex == 1) {
//Do Nothing
}
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:#"pushViewIdentifier" sender:self]) {
ChatViewController *cvc = (ChatViewController *)segue.destinationViewController;
cvc.property = self.someProperty;
}
}
I have a custom cell with a UITextField . Where I should set the delegate, in the custom cell class or in the view's class ?
I was trying to do this in both, but no results. I set the delegate from storyboard.
Here is what I did:
-(void)textViewShouldReturn:(UITextField *)textField
{
if ([textField.text isEqualToString:#""]){
return;
}
UIAlertView *helloEarthInputAlert = [[UIAlertView alloc]
initWithTitle:#"Name!" message:[NSString stringWithFormat:#"Message: %#", textField.text]
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[helloEarthInputAlert show];
}
But this part is working if i put this cell.noteTextField.delegate=self :
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
self.tableView.frame=CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y-30, self.tableView.frame.size.width, self.tableView.frame.size.height);
return YES;
}
Add the delegate in the custom cell's class. But do not forget to implement the protocols
// MyCell.h
#interface MyCell : UITableViewCell <UITextFieldDelegate>
// ...
// MyCell.m
-(void)autoconfigureCell // a method I added to configure cell, but also can do in "-(void)layoutSubviews"
{
// ...
myTextField.delegate = self;
// ...
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField // <-- BOOL
{
// do stuff
return YES;
}
You should set the delegate in your ViewController, it has to implement UITextFieldDelegate and to know what textfield is calling the delegate methods, you can do something like this:
-(void)textViewShouldReturn:(UITextField *)textField
{
CGPoint textFieldPosition = [textField convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:textFieldPosition];
...
}
i want to clear all my textfields when clicking cancelButtonTitle: on action sheet;
here is my action sheet code:
- (IBAction)cancelButtonPressed:(id)sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:#"Are you sure?"
delegate:self
cancelButtonTitle:#"No Way!"
destructiveButtonTitle:#"Yes, I’m Sure!"
otherButtonTitles:nil];
[actionSheet showInView:self.view];
}
Maybe you haven't implemented UIActionSheet delegate:
#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == actionSheet.cancelButtonIndex) {
//Clear textfiled at here
self.firstNameField.text = #"";
}
else{
//Do somethings for #"Yes, I’m Sure!"
}
}
In addition to simalone's answer, which states you will need to implement the following action sheet's delegate method:
#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == actionSheet.cancelButtonIndex) {
//Clear textfileds at here
}
else{
//Do somethings for #"Yes, I’m Sure!"
}
}
If you wanted something a bit automatic, you could create a category on UIView, and implement the following recursive method:
UIView+ClearTextFields.h
-(void)clearAllTextFields;
UIView+ClearTextFields.m
-(void)clearAllTextFields
{
if ([self isKindOfClass:[UITextField class]])
{
[(UITextField*)self setText:nil];
}
for (UIView* subview in self.subviews)
{
[subview clearAllTextFields];
}
}
If you needed to also clear all TextViews, you could add the following to the above method:
if ([self isKindOfClass:[UITextView class]])
{
[(UITextView*)self setText:nil];
}