Xcode - issue of push view by segue - ios

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;
}
}

Related

I could not able to call one IBaction on click of alertView button

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.

Change alert button to open screen

I currently have an alert button which I would like to change, so instead of clicking a cell to go inside it I would like use the button.
- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index
{
switch (index) {
case 0:
{
NSLog(#"More button was pressed");
UIAlertView *alertTest = [[UIAlertView alloc] initWithTitle:#"Hello" message:#"More more more" delegate:nil cancelButtonTitle:#"cancel" otherButtonTitles: nil];
[alertTest show];
[cell hideUtilityButtonsAnimated:YES];
break;
}
How can I integrate that with this:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BNRDetailViewController *detailViewController =
[[BNRDetailViewController alloc] init];
// Push it onto the top of the navigation controller's stack
[self.navigationController pushViewController:detailViewController
animated:YES];
}
If you mean that you want the button that is revealed when the user swipes the tableview row to push the viewcontroller instead of displaying an alert view, this is what you want to do:
- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index
{
switch (index) {
case 0:
{
BNRDetailViewController *detailViewController = [[BNRDetailViewController alloc] init];
// Push it onto the top of the navigation controller's stack
[self.navigationController pushViewController:detailViewController
animated:YES];
[cell hideUtilityButtonsAnimated:YES];
break;
}
If you mean to push the new controller with a click from the alert view, you'll have to add the UIAlertViewDelegate and a message for the other button on your alert view. Then call the clickedButtonAtIndex Method
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex == [alertView firstOtherButtonIndex] && [alertview.title isEqualToString:#"Hello"]) {
[self.navigationController pushViewController:detailViewController
animated:YES];
}
}

UIActionSheet button segue into new ViewController

I'm trying to make the buttons on a UIActionSheet call segues which go to other view controllers. This is the code i have so far and it just crashes. The error message i get is "'Receiver () has no segue with identifier 'Town''"
- (IBAction)CategorizeButton:(id)sender {
UIActionSheet* AS = [[UIActionSheet alloc]initWithTitle:#"Group by"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Town",#"Title",#"Location", nil];
[AS showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
[self performSegueWithIdentifier:#"Town" sender:self];
}
if (buttonIndex == 1)
{
[self performSegueWithIdentifier:#"Title" sender:self];
}
if (buttonIndex == 2)
{
[self performSegueWithIdentifier:#"Location" sender:self];
}
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSString* CategorizeTown = #"Town";
NSString* CategorizeTitle = #"Title";
NSString* CategorizeLocation = #"Location";
if ([[segue identifier] isEqualToString:CategorizeTown])
{
CategoryTableViewController* CAT = [segue destinationViewController];
// blah blah
}
if ([[segue identifier] isEqualToString:CategorizeTitle])
{
CategoryTableViewController* CAT = [segue destinationViewController];
// blah blah
}
if ([[segue identifier] isEqualToString:CategorizeLocation])
{
CategoryTableViewController* CAT = [segue destinationViewController];
// blah blah
}
}
In storyboard, select the segue.
Give it an identifier, (in your case, #"Town",#"Title",#"Location")

how to disable firstOtherbutton of alertview

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

set all textfields to blank when clicking cancel button of the action sheet (iOS)

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];
}

Resources