I have a simple UIButton in a viewController.
- (IBAction)btnEnter:(id)sender {
..
..
}
Now I want to trigger the button press action programatically one of library functions (didFinishPickingmediaWithInfo) gets executed
- (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info
{
// Do Something
txtBoxAboveEnterButton = #"Data populated successfully";
[self btnEnter:nil];
[reader dismissModalViewControllerAnimated: YES];
}
The function is getting called and the text box above Enter button is getting populated with the dummy text but click is not getting triggered. Please help me out!
The method you are calling is btnEnter with no parameter. It doesn't look like you have that defined. Try passing a paramter like nil or self to it:
[self btnEnter:nil];
Related
I have two views in my app and a plist file to store some values.
In the first view I've created a button called frequenciesButton that opens the second view and another button to restore the default values.
In the second view there is a pickerView and a "Done" button.
On the .m of the first view:
- (void)viewDidLoad {
[super viewDidLoad];
//
self.gameSettings = [[NSMutableDictionary alloc] initWithContentsOfFile:gameSettingsFilePath];
}
-(void)viewWillAppear:(BOOL)animated {
[self refreshView];
}
- (void)refreshView {
[self.frequenciesButton setTitle:[NSString stringWithFormat:#"%# hz and %# hz", [self.gameSettings objectForKey:#"freq-freq1"], [self.gameSettings objectForKey:#"freq-freq2"]] forState:UIControlStateNormal];
...
}
- (IBAction)setDefaultValues:(UIButton *)sender {
[self.gameSettings setValue:#880 forKey:#"freq-freq1"];
[self.gameSettings setValue:#1122 forKey:#"freq-freq2"];
...
[self.gameSettings writeToFile:gameSettingsFilePath atomically:YES];
[self refreshView];
}
When the first view is loaded, the button title is changed to the default values stored in the gameSettings dictionary. The method setTitle: works.
When I click on the frequenciesButton it opens the second view with the pickerView, I select the two new values for the freq-freq1 and freq-freq2 and it saves to the plist file on done button.
The problem is that the frequenciesButton title is not changed when the second view is dissmissed and the first view appears. The refreshView method is called but the button setTitle: does not work.
In this case, if I go back one screen, and return to this view, the button title is updated.
And when I click on defaultValuesButton, the frequenciesButton title changes. The method setTitle: also works.
Any ideas of what must be happening?
HaHA! I love that you added a link to your project.
SO!! The problem was that you have separate properties in each view to hold the data from the saved plist file, self.settings. This is fine, don't mesh them together. The requirement you had to do with this, when switching views, is to keep the ivar or properties updated as the data updates too :D
Here is how I fixed the problem:
- (void)viewWillAppear:(BOOL)animated {
self.settings = [NSMutableDictionary dictionaryWithContentsOfFile: filePath];
[self updateView];
}
I checked out the file and that was updated, but the dictionary in the TestViewController.h was not updated
I hope this was the problem :)
One problem there, not sure if it will fix it, is the fact that you have used ViewWillAppear incorrectly, you have this:
-(void)viewWillAppear:(BOOL)animated {
[self refreshView];
}
but it should be this:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self refreshView];
}
You need to invoke "[super viewWillAppear:animated];" or you will have side effects, fix that first and see what happens.
I have a UITableView that i would like to hide until the user taps the button searchButtonTapped. (I'm also using this button as an IBAction.)
Originally i'm hiding the table view as you see in the viewDidLoad, and i wanna show it after the button was tapped, but it does not shown up after i tap the search button. Do i missed something? For me, it seems it should be work properly, after the button was tapped i refresh the table view.
my .h file
#property (weak, nonatomic) IBOutlet UIButton *searchButtonTapped;
- (IBAction)searchButton:(id)sender;
.m file
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.hidden = YES;
}
- (void)buttonTapped:(id)sender {
if (sender == self.searchButtonTapped) {
self.tableView.hidden = NO;
[self.tableView reloadData];
}
}
- (IBAction)searchButton:(id)sender {
[self searchSetup];
}
It's impossible to tell from the little bit of code that you posted. Add NSLog statements in your buttonTapped method that show entering the method, entering the if statement, the value of searchButtonTapped, and the value of self.tableView.
Then you can tell if the method is getting called, if the if statement is evaluating as true, and if the table view is non-nil. One of those things is likely to be the cause of your problem.
I'm guessing that the if statement is wrong. what type is the property self.searchButtonTapped? Post the code that declares that property.
Based on the name I would guess that searchButtonTapped is a boolean?
you have declared only one IBAction, which is for the method searchButton.
This method call the searchSetup´s method. What is the purpose of it?
- (IBAction)searchButton:(id)sender {
[self searchSetup];
}
So you must have another IBAction for buttonTapped method witch is currently a "void" method and not a IBAction. Or you make that connection from the storyBoard, or you must declare it programaticly like:
[self.searchButtonTapped addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]
I'd like to achieve the following behaviour of a single UITextField and the keyboard:
when the view has loaded UITextField becomes first responder and the keyboard opens (so far so good):
- (void)viewDidLoad
{
[self.editField becomeFirstResponder];
self.editField.delegate = self;
}
Now user inputs some text and presses the return key. That text is added to the data source in the following method:
- (IBAction)didEndOnExit:(id)sender {
//add self.editField.text to data source
}
Now after the return key is pressed and the above UITextField's method gets called and executed I would like the UITextField to clear, the cursor to be placed and be visible at the beginning of the text field and the keyboard not to hide so that new item could be entered in the textfield and added to the data source.
This is how I return to the previous view in the app (using a button):
- (IBAction)backButtonPressed:(id)sender {
[self dismissViewControllerAnimated:NO completion:^{}];
}
I tried playing with UITextFieldDelegate's methods and UITextField's becomeFirstResponder and resign FirstResponder, but am unable to achieve the above described behaviour. I've seen posts here on Stack Overflow about using consecutive UITextField's to enter data, but not to use the same UITextField time and again.
You should override the below mentioned method and do whatever you want and showing the keyboard too.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
Have a try :)
While dismissing view controller all other code executions freezes when (view controller is being dismissed).
Use some delay (performselectorafterdelay). or
completionhandler of dismissingviewcontroller for executing your code when viewcontroller is being dismissing and
Also execute code on main Thread which includes UI changes or updates
after saved your data.. you have to make the uitextfield to be nil... like self.editField.text = nil. then you can call [self.editfield becomesfirstresponder].. after you will add more items to array or else anything everytime the field should be nil after you added..
From the Doc
Return Value: YES if the text field should implement its default
behavior for the return button; otherwise, NO.
For EX - Use the Delegate of UITextField :
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//add self.editField.text to data source
textField.text = #"";
return NO; // This will do your need
}
Because I used UITextField's action method - (IBAction)didEndOnExit:(id)sender the keyboard always got closed. Apparently that method makes the UITextField resign from being first responder. Instead of adding data to data source in that method, it can be added in the delegates's method - (BOOL)textFieldShouldReturn:(UITextField *)textField and so the keyboard won't close.
I am using a uipopover to present a mini number pad to the user when they enter a textfield on my main view controller.
when they enter numbers using the number pad, i save the entry into a nsstring property that I've named keypadvalue.
there is an unwind segue wired to a done button on the popover which fires the following code.
- (IBAction)doneWithKeyboard:(UIStoryboardSegue *)segue
{
NSLog(#"unwind");
if ([segue.sourceViewController isKindOfClass:[KeyPopupViewController class]])
{
KeyPopupViewController *popOver2 = segue.sourceViewController;
activeField.text =popOver2.keypadValue;
}
}
the activetextfield on my main view controller then gets updated to the kepadvalue, and this all works fine.
my problem now is that i want the activetextfield to update the same way if the user presses outside the uipopover, and it dismisses without firing the unwind segue.
i thought i might use the following to perform the update when the popover dismisses
-(BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController
{
activeField.text = controller.keypadValue;
return YES;
}
unfortunately despite multiple attempts i can't get the property to return a value it is always null even though the method fires as expected.
how should i recover the property value from the popover using this or another method?
i am obviously doing something wrong
can anyone advise
thanks
It should help:
-(BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController
{
[self.view endEditing:YES];
activeField.text = controller.keypadValue;
return YES;
}
essentially I'm deleting pictures inside of an app.. everything is coded etc.. etc.. so when the user holds the UIButton the image is swapped and has the X over it. Click again and image is deleted from the Doc Directory and DB. So it only made sense to add an alertview before deletion.. problem is that the methods I'm using use (UIButton*)sender as a parameter. I need to pass that parameter to the next method to property delete from the screen.
Is there a relatively simple way to do this..
this is the function that calls the deletion.. the function that would initiate the alertview is also returns a void and takes the same UIButton.
-(void)action:(UIButton*)sender {
if (edit == true)
{
[sender removeFromSuperview];
[[scrollView viewWithTag:[sender tag]] removeFromSuperview];
[self deleteFromDoc:sender];
edit = false;
stop = false;
NSLog(#"remove");
}
}
Change your method to
-(void)action:(UIView *)sender
Assign the tag of the uibutton to the alertview, then in didClickButtonWithIndex: call your action: method passing in the alertView as sender.