I am new to iOS programming and using iOS 6. I see that, using XCode I can CTRL+drag action and outlets for button sand text field but not for the storyboard. I want to do some action when user clicks on the storyboard (taps away from the text field).
Here is my code:
- (IBAction)editingEnded:(UITextField *)sender {
NSLog(#"%#", #"in editingEnded");
[sender resignFirstResponder];
}
- (IBAction)buttonSelected:(UIButton *)sender {
if(_firstClick) {
[_textField resignFirstResponder];
}
}
I guess, you asked me to implement something like editingEnded? This is my delegate for editing did end action (how can I confirm this, there is no such annotation/attribute attached to this method?). However, this method alone didn't work. When i added the 2nd method buttonSelected as a delegate for another button on the story board, then editingEnded is also called due to [_textField resignFirstResponder];.
A storyboard is a container which contains your various UI elements, including your text field.
What you really want to do is set a delegate for your text field, which you can do with your view controller.
Then, when you click away from the text field, you can catch that happening via the delegate method "textFieldDidEndEditing:". You implement that function in your view controller, make certain your text field has your view controller set as the delegate, and you should be able to do whatever you want within your view controller's implementation of the "textFieldDidEndEditing:" function.
Related
I am using https://github.com/CEWendel/SWTableViewCell library to my project.
Certain situation I need to disable the particular button action of swipe cell.
I cannot find any property in their class file. If anyone crossed this, give me answer.
Here I have attaced my swipe options image:
For ex
: I want to disable the share button action.
Let's assume your share button is in the leftButtonsArray. In the method:
- (void)swipeableTableViewCell:(SWTableViewCell *)cell scrollingToState:(SWCellState)state
{
//case:left buttons opened
UIButton *shareButton = leftButtonsArray[theIndexOfTheShareButton];
shareButton.enabled = NO;
}
#karthikeyan You can hide the button for a particular row in tableview by the following code:
- (void)updateRightUtilityButtons:(NSArray *)rightUtilityButtons WithButtonWidth:(CGFloat) width {
_rightUtilityButtons = rightUtilityButtons;
[self.rightUtilityButtonsView updateUtilityButtons:rightUtilityButtons WithButtonWidth:width];
[self.rightUtilityButtonsView layoutIfNeeded];
[self layoutIfNeeded];
}
Add/update this methods to SWTableViewCell.m class, where rightUtilityButtons is an array of buttons you need to display for the particular row.
In case if you want to disable just user interaction you can achieve while adding button into array, just disable user interaction for that button by shareButton.userInteration = NO and then add to array and then pass the array to the method defined above. By this you can be sure that button is disabled.
But please provide the sample code that you have worked so that can update your code directly.
In case if you still didn't get revert back I'll give you the working code directly here.
I have a relatively large iPhone application with many views.
I would like implement a next/previous option on my keyboard.
I have managed to implement it UI-wise, with some code examples i saw online, but all of them are assuming we need to add code to each view controller to implement the actual transition between the text fields.
My question is: is there a general way to know, given some text field, who is the next field in order? (i.e without refactoring each of my view controllers)
I ask this question because when i use the iPhone simulator and press the computer's Tab key - the switch between the fields happen, so i wonder if there is a built-in or generic way to implement it on iOS.
clarification:
is there a way of doing it without adding a specific code for each type of view controller? (adding a generic code is acceptable)
I want to write how i solved this problem, with the help of many good answers given to me here :)
First, i could not create fully generic code that creates tab regardless of the view it is in.
Instead i created this thing, which i think is the most generic solution with the firstResponder method not working:
i created custom toolbar with my next/previous/done buttons and appropriate actions delegate.
than i extended UIViewController by adding category "Tab". this category declares a fieldsArray and implements the delegate method.
Now what every specific view controller needs to do (beside importing the category) is to provide this fieldsArray according to its properties and calling the init method which adds the buttons toolbar to this fields
I hope you could benefit from this, and again thanks for all the good answers
you could have a method in a utility class that takes as arguments a textfield and a viewcontroller. then you could use the "tag"-attribute of the textfields to find the next textfield in that viewcotroller, assuming that you assigned the tags accordingly. numbers would be great, i think. a simple callback method in the vc could handle the focus-change. thats about as generic as i can see right now.
This is some generic code that I came up with:
// add a property for the fieldsArray
//add this in viewDidLoad
_fieldsArray = [[NSMutableArray alloc] init];
NSArray *viewsArray = [self.view subviews];
for (id view in viewsArray) {
if ([view isKindOfClass:NSClassFromString(#"UITextField")]) {
[_fieldsArray addObject:view];
}
}
//add this in your action that switches the fields
for (UITextField *field in _fieldsArray) {
if ([field isFirstResponder]) {
if ([fieldsArray lastObject] == field) {
[_fieldsArray[0] becomeFirstResponder];
}else {
NSUInteger nextIndex = [_fieldsArray indexOfObject:field] + 1;
[_fieldsArray[nextIndex] becomeFirstResponder];
}
break;
}
}
Before using it it should be improved.
1) find all subviews of self.view recursively
2) do some checks if the arrays are empty or nil or have just one object in them.
Good luck!
I am practicing ios and I am trying to make simple real estate app, one viewcontroller, I put navigation bar and at the right sie of navigation bar I have button map
(I am trying to show all houses in area with basic data:street, price, long, lat... in tableview and just like marker on mapview).
I want that button to behave like toggle, to switch between table view and map view but in same controller. ( In android I could put one below other and just alternately set visibility to gone to one and visible to another).
How to alternately switch views in same controller (view need to be all over screen) ?
just assign the newView to oldOne
UIView *generalView=[UIView alloc]init];
[self.view addSubView:generalView];
when you want to show tableView ,then just assign
generalView=tableView;
and when you want map, then
generalView=mapView;
Are you using a storyboard? Why not just perform a segue on button tap?
- (void)myButtonMethod
{
//execute segue programmatically
[self performSegueWithIdentifier: #"MySegue" sender: self];
}
"MySegue" would be the segue identifier that is set in the storyboard between the two views.
Add two views tableview and maple to UIViewController's view.
It would be like:
-(void) viewDidLoad
{
[self.view addSubview:tableView];
[self.view addSubview:mapView];
}
On button click decide which view needs to be displayed:
-(void) onButtonClick:(UIButton *)sender
{
//If you want to display map..
[self.view bringSubviewToFront:mapView];
//If tableview needs to display
[self.view bringSubviewToFront:tableView];
}
I have an iPad App that I want to be compatible from iOS 5.0 to 6.0. My main view contains a scroll view z-indexed on the front, which is initially set to hidden. I also have a toolbar containing a button that cycle the scroll view hidden or not.
I would like to add a feature to present the scroll view as initially visible when the user opens the App for the first time to make the help visible by default to new users.
My code to cycle between visible and hidden is the following:
- (void)showHelpView:(id)sender {
BOOL hidden = [blackTranslucent isHidden];
[self.view bringSubviewToFront:scrollViewOutlet];
if (hidden) {
[scrollViewOutlet setHidden:FALSE animationStyle:KGAnimationFade duration:0.7];
[blackTranslucent setHidden:FALSE animationStyle:KGAnimationFade duration:0.5];
}
else {
[scrollViewOutlet setHidden:TRUE animationStyle:KGAnimationFade duration:0.5];
[blackTranslucent setHidden:TRUE animationStyle:KGAnimationFade duration:0.7];
}
}
where the sender is my toolbar button, blackTranslucent is a view on top of the main view and scrollViewOutlet is my scroll view IBOutlet.
Add a property "isNewUser" on NSUserDefaults in the application:willFinishLaunchingWithOptions: method which will only be written once by checking if the key exists.
In your main view in the viewDidLoad check this property if true make the view visible and update the key to false. if not just continue regularly.
Further information on NSUserDefaults
Hope that helps
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Easy way to dismiss keyboard?
In view1, the keyboard gets shown. I want to dismiss the keyboard when i move to another view (view2).
In view2, in the method that gets called when the view is shown, i try to do so:
[self endEditing:YES];
But this doesn't work, so i get the idea of catching the event (in View1) of a hidden view and dismiss the keyboard before moving to view2. Is this possible?
EDIT:
I think i need to clarify that view2 is not fully hidden when view1 is shown. It's 50 shown vertically.
As long as i work on view1, the Keyboard is shown and view2 is also shown (50%). view2 has a button, when i click on that button, i need to dismiss the keyboard (which is shown from view1).
I tried to mplement a method in view1 like so:
-(void)dismissKeyBoard{
[self endEditing:YES];
}
And call the method above in view2 when i click on the button but it doesn't work.
PS: The button i click in view2 will make view2 shows fully (100%) above view1.
To dismiss the keyboard, you just have to "resignFirstResponder" on the input field that it is currently in. One easy way is to have a catch all method of all the input fields in your view.
So for example, I usually create a method like this:
-(void)dismissKeyboard {
[self.textfield1 resignFirstResponder];
[self.textfield2 resignFirstResponder];
}
then just call it before you transition
[self dismissKeyboard]