Navigation through the textfields which are each inside successive tableView cells? - ios

I am using a tableView Controller and in it I have 4 sections. 1st section contains 2 textfields, 2nd one has 4 textfields, 3rd one has 7 and so on. The problem which I am facing is that I am not able to navigate through the textfields by pressing the next and previous buttons on the keyboard. Actually I have tried a lot of open source libraries to put "Next" and "previous" button on a toolbar above the keyboard but not been able to do so. Every time the keyboard loads there is no toolbar above it.
Maybe It is due to the textfields inside the tableview cell.
Can anybody suggest me the solution to do that.

Try:
UIToolbar* toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
toolbar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *barItem = [[UIBarButtonItem alloc]initWithTitle:#"Previous" style:UIBarButtonItemStyleDone target:self action:#selector(prev)]
toolbar.items = [NSArray arrayWithObjects:[[UIBarButtonItem alloc]initWithTitle:#"Next" style:UIBarButtonItemStyleDone target:self action:#selector(nextTF)],barItem,nil];
[toolbar sizeToFit];
self.yourTextField.inputAccessoryView = toolbar;
Next do:
-(void)prev
{
//previous
}
-(void)nextTF
{
//next
}

Related

IOS - Add button in tool bar left and middle

I want to create a UI tool bar with effect like this
[DONE]-------[MINUS]--------
button DONE locate at left and button MINUS locate at middle
this is my code but I didn't get the button MINUS set to middle
UIToolbar* mtbKeyboardAccessoryView = [[UIToolbar alloc] init];
[mtbKeyboardAccessoryView sizeToFit];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(doneKeyboard)];
UIBarButtonItem *minusBtn = [[UIBarButtonItem alloc] initWithTitle:#"MINUS" style:UIBarButtonItemStyleBordered target:self action:#selector(minusSign)];
UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target: nil action: nil];
[mtbKeyboardAccessoryView setItems:[NSArray arrayWithObjects:doneBtn, flexibleItem, minusBtn, flexibleItem, flexibleItem, nil]];
I don't want to create button with frame and customary set (w, h, x, y) and add sub-view to UI tool bar
Is that any way to set the UI bar button at left and middle and without affect the button coordinate when change orientation in landscape and portrait
I get a result like this
[DONE]-------[MINUS]-------------------
which minus button not set to middle
Add UIBarButton for Done than add FlexibleItem than another UIBarButton for Minus and again add FlexibleItem.
Try to add this above..!! May this will help you..!!

How to align UIBarButtonItem in UIToolbar in iOS universal app?

I want to put two UIBarButtonItem instances to UIToolbar in my iOS universal app, on which the first item is on the left-edge of the toolbar and the second is on the center. However, it looks like this cannot be done in Interface Builder, because the auto layout doesn't support forcing constraints on UIBarButtonItem class due to the fact that it is not a subclass from UIView.
Notice that I don't want to put the first on the left-edge and the second is on the right-edge - I have to put the second button at the center. Also, I don't have a plan to use the third button in order to align them as left, center, and right at equal interval.
Is it still possible to add such constraint in this situation in storyboard? I use Xcode 6 beta 5.
You can do it all in your Storyboard.
Select a Bar Button Item in the Object library and drag it into your Toolbar. Add a Flexible Space Bar Button Item at its right. Add your second Bar Button Item at its right. Then, add a second Flexible Space Bar Button Item at its right.
The result will look like this in Interface Builder:
If necessary, you can add an extra Fixed Space Bar Button Item at the right edge of your Toolbar to be sure that your second Bar Button Item is really centered.
User lxt gives another example of this in answer for a similar question here.
Edit: Be sure that your UIToolbar has good constraints before proceeding!
you can do this programmatically
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:#"item1" style:UIBarButtonItemStylePlain target:self action:nil];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:#"item2" style:UIBarButtonItemStylePlain target:self action:nil];
UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithTitle:#"item3" style:UIBarButtonItemStylePlain target:self action:nil];
self.toolbarItems = [NSArray arrayWithObjects: item1, flexible, item2, flexible, item3, nil];
check this link if you want more details
How to place UIBarButtonItem on the right side of a UIToolbar?
I think you should try Flexible bar button space item and for more understating i am sharing a link, so have look of it might this help you out
UIToolbar and Flexable/Fixed bar button items
Code to add toolbar :
Here fixed width can be variable. Accordingly you can keep it as far as you want so as to keep the second button in centre.
UIBarButtonItem *fixedItemSpaceWidth = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixedItemSpaceWidth.width = 200.0f; // or whatever you want
UIBarButtonItem *doneBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonAction:)];
UIBarButtonItem *cancelBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelButtonAction:)];
// Initialise toolabr
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-44, 320, 44)];
//toolbar.barStyle = UIBarStyleBlack;
toolbar.items = [NSArray arrayWithObjects:cancelBarButton,fixedItemSpaceWidth,doneBarButton, nil];
[self.view addSubview:toolbar];

iOS 7.1 update hide navigation bar left button by default

I have following code to create UINavigationBar and set the navigation item with a back button at the right side.
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 60)];
navBar.delegate = self;
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleDone target:self action:#selector(backButtonTapped)];
UINavigationItem *backItem = [[UINavigationItem alloc] init];
[backItem setTitle:#"What's New"];
[backItem setLeftBarButtonItem:back];
backItem.leftBarButtonItem.enabled = YES;
[navBar pushNavigationItem:backItem animated:NO];
[self.view addSubview:navBar];
This worked perfectly until i update my xCode 5 to iOS 7.1 update recently.
But now when the UIView presented the navigation button is not visible. But when i touch the location of the button (where it used to be before the update), it shows me the button and click even is firing.
My question is how to set the button visible at the moment view is present to user ?
Thank you.
Try to initialize your UIBarButtonItem with initWithCustomView. Also, try to set buttonType of the UIButton to UIButtonTypeSystem.
Thanks to all,
finally what happen is as follows.
The above behaviour is not what i assume it is. When a new view is loaded the button is disappear by default and only if i move my finger here & there on the location button used to be it appears. Further when i keep the view still, after it loaded, about 15 seconds, button appears.
So i suspect this is with "viewDidLoad" method. (Code related to this UINavigationBar is in viewDidLoad method.
When I move above code to "viewDidAppear" method it start to work again.

How to add UINavigationBar with two UIBarButtonItem programmatically?

I added code to the viewDidLoad method of ViewController. I have two UIBarButtonItem, one of them is on the left called Left and the other one is at the right called Right. I can get the left button to work but I cannot even see the right button. What am I doing wrong?
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 420, 44)];
[navbar setBackgroundImage:[UIImage imageNamed:#"bg_fade.png"] forBarMetrics:UIBarMetricsDefault];
UINavigationItem *item = [[UINavigationItem alloc]initWithTitle:#"Tray Tracker"];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithTitle:#"Left" style:UIBarButtonItemStylePlain target:self action:
#selector(leftAction:)];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithTitle:#"Right" style:UIBarButtonItemStylePlain target:self action:
#selector(saveAction:)];
item.leftBarButtonItem = leftButton;
item.rightBarButtonItem = rightButton;
navbar.items = #[item];
[self.view addSubview:navbar];
I do have the IBActions for leftAction and saveAction
If you simply want a bar with two buttons, you should use a UIToolbar instead. It's much easier to handle for this simple case. The only change you would need is to create a UIBarButtonItem flexibleSpace (it's a system item), and call [toolbar setItems:#[leftButton, flexibleSpace, rightButton]];
If you need actual navigation functionality such as a back button, take a look at this answer. You need to use UINavigationBar pushNavigationItem.
using UINavigationBar without UINavigationController
In your example, you are misusing the items property. The documentation for UINavigationBar states,
The bottom item is at index 0, the back item is at index n-2,
and the top item is at index n-1, where n is the number of items in the array.
In order to do accomplish what you're trying to do with a UINavigationBar, I think you need multiple navigationItems.
It would be MUCH easier to either use a UIToolbar or use the UINavigationBar in conjunction with a UINavigationController.
Edit
Now that you posted the image, I think the reason you can't see the right bar button is because your frame width is too large. The iphone screen is 320 px wide, so your frame for the navigationBar should be CGRectMake(0, 0, 320, 44)

UISegmentedControl buttons stop working

My iOS app has a form with about a dozen fields and I wanted to add "Previous"/"Next" buttons above the keyboard to help users move between fields. I have something like this:
for (UITextField *t in _textfields) {
t.delegate = self;
UIToolbar *toolbar = [[UIToolbar alloc] init];
[toolbar setBarStyle:UIBarStyleBlackTranslucent];
UISegmentedControl *segControl = [[UISegmentedControl alloc] initWithItems:#[#"Previous", #"Next"]];
[segControl addTarget:self action:#selector(switchTextField:) forControlEvents:UIControlEventValueChanged];
segControl.segmentedControlStyle = UISegmentedControlStyleBar;
segControl.tag = [_textfields indexOfObject:t];
if (segControl.tag == 0) [segControl setEnabled:NO forSegmentAtIndex:0];
else if (segControl.tag == [_textfields count] - 1) [segControl setEnabled:NO forSegmentAtIndex:1];
[segControl sizeToFit];
UIBarButtonItem *segItem = [[UIBarButtonItem alloc] initWithCustomView:segControl];
UIBarButtonItem *flexButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *doneButton =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self.view action:#selector(endEditing:)];
[toolbar setItems:#[segItem, flexButton, doneButton]];
[toolbar sizeToFit];
[t setInputAccessoryView:toolbar];
}
The problem I'm having is that every so often one of the segments stops working. The method switchTextField just does not get called, the button looks normal, not disabled, but the touches do not register. Sometimes it's the "previous" button, sometimes it's the "next" one, never both at the same time, and sometimes they both work fine. Once one of the buttons gets stuck, the only way to (temporarily) fix it is to tap another textfield without the use of the toolbar or dismiss the keyboard and start editing again. What could be the problem?
A segmented control is not a good choice when you really want the behavior of a UIButton for your prev/next controls.
What I think is happening is that you are initializing the toolbar with the segmented control and no segment selected. So when you tap prev/next the first time, the value changes and that segment is selected for that specific segmented control. As you continue to tap prev/next, eventually you get to a segmented control that already has a segment selected and that segment will not fire a value changed event when tapped because it is already the selected segment.
You can explicitly deselect the selected segment in your switchTextField: method (make sure you don't trigger switchTextField: recursively) or you can change your implementation to use UIButton objects for prev/next instead of the segmented control.
I recommend going with UIButton.

Resources