Adding Search Button to UINavigationBar : Swift - swift

I have a ViewController1 and ViewController2. ViewController1 has a NavigationController set to it. I have a button (Custom Search Icon) on the NavigationBar. Now I press control+click and drag to ViewController2 and set it to Show. When I press it, it doesn't work. I also tried to drag the button to ViewController1 and set it as an IBAction but that doesn't work either.
What I want accomplished:
I want to create a button on the left side of the NavBar. When clicking it, I want to be add the searchbar and allow the user to search from an array. What is the best way about doing this? The search bar also has the cancel button, and if pressed the user should return back to his previous ViewController. I don't want anyone to the work for me but if you could point me to the right direction, it would be awesome.

I'm assuming by "Custom Search Icon", you mean a UIBarButtonItem in the navigation bar. If that's the case you should be able to segue to a controller using the technique you described, so you could try putting in a new UIBarButtonItem and try again.
But for the problem you're describing I might recommend a slightly different approach using UISearchController. Since it sounds like you're using storyboards, drag in the Search Bar and Search Display Controller from the object library to your controller, just below the navigation bar.
Check out UISearchController documentation:
[https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UISearchController/ ]
And here's a sample project from Apple:
[https://developer.apple.com/library/ios/samplecode/TableSearch_UISearchController/Introduction/Intro.html ]
Good luck!

Delete all the connections from the interface builder first. Clear the codes associated to the button.
Create an IBAction and put a break point to see if its called when u click the button.
If it does get called then push a new ViewController onto it,(ViewController2).
Add your search bar here on the ViewController 2.

Related

Why is the IB Action set to exit as object?

I am creating an app that requires notifications. I created a settings page with a switch to toggle notifications for the application. I tried to link the switch to ViewController2 using an #IBAction, but it keeps connecting the object to Exit, instead of ViewController. I am getting really frustrated because the #IBAction won't stay connected. I am not sure what is going on, but if I could get help that would be amazing.
The images are linked down below to Imigur.
This is the connection to ViewController2
This is what occurs when I connect the IBAction to ViewController2
You can see there is no action option for the connection in this image.
When you attach #IBActions (or #IBOutlets), you do not want to attach them to Exit on the View Controller.
Follow these steps to add an #IBAction:
1) Make sure your View Controller's class is linked to your file, then go into the Assistant Editor making sure you selected Automatic.
2) Hold Control (^) and drag the button to your ViewController class.
3) Add the button as an #IBAction. - It's also recommended to use UIButton as the sender, instead of Any.
It should look like this:
If you have questions, let me know!
The problem might be that you are supposing you can form an action connection from a UISwitch in the scene of one view controller to a different view controller. You can't. You can form outlets and actions only between a view controller and the interface inside the same scene of the storyboard.
Click on the top bar of your problematic ViewController (the overview of what is getting displayed on screen), click the top left icon (called View Controller).
On the right click "Show The Identity Inspector" icon and you should see class. Change the text to 'ViewController'. Hopefully it should auto fill.
This will solve the problem.

How can I add a button to the top of my TableView

I have the following in my storyboard and would like to add a button at the top of the TableView that allows me to add Members.
The scenario, a user is presented with the first screen which displays 'User Profile' and 'Members'. When 'Members' is touched the next screen is displayed that shows a list of Members and at the top is a button that says '< Settings' whcih allows the user to go back one screen.
I am trying to display a 'Add' button at the top when the list of Members is displayed.
Hope that makes sense!! I was hoping to not have to Embed the TableView in another Nav Controller.
Thanks for any help!!!!
You can add a button into the navigation bar, but first, you need to have a navigation bar.
To do this, you need to select your scene in the Storyboard and then go to the the Editor menu and select Embed In:Navigation Controller.
Once this is done, you can drag in a Navigation Item from the panel on the right.
Then you can add that to your class as an IBOutlet and declare an IBAction for it.
Cool?
Also, I think that your storyboard file might not be set up right.
Check out Apple's Simple Drill Down sample file available here:
http://developer.apple.com/library/ios/samplecode/SimpleDrillDown/Introduction/Intro.html
Cheers.

How to select tab in uiTabBarController using a custom center button

I have a storyboard enabled App with a TabBarController where I have added viewcontrollers through the interface builder.
Then I followed the iDevReceipes post to add a custom button in the center, which hide my third or center tab bar item with a custom button.
I'm not able to make the TabBarController select the middle tab when a user clicks the button. I wired the button with a selector and within the method I added a code which was supposedly a solution which worked for many on Stack Overflow i.e self.tabBarController.SelectedIndex = 2; this was called in viewWillAppear
This is somehow not working and selecting the correct tab. I had a NSLog inside the method of the button and I found out the button works but the tab is not getting selected.
The tabbarcontroller has a class with nothing inside this.
You have the right idea, but you need to use a setter method and not set the property directly; this is one instance where it does make a difference. Also, I may be misunderstanding you, but you need to call this code in the selector for the button, not in the viewWillAppear method. Here's the code you'll use to select the middle index.
[self.tabBarController setSelectedIndex:2];

Add a custom back button to Navigation Bar using MonoTouch

Just want to confirm this, as I'm trying to learn monoTouch alone..
I have a view which I navigate to using NavigationController.PushViewToController(). On the destination view, I have a Navigation Bar. I can add a button to the bar and use code to push to another view (I happen to know where Back is), fine.
Is there a existing "back button" control? Or a way in to code to change the existing back button to say "Go back"?
In Interface Builder I can see there is a property on the navigationItem called "Back". When I add text to this I can see a new BarButtonItem added to the navigationBar. However I never see this button when I navigate to the view in the simulator. If I try to drag the item onto the view manually, the "Back" text is cleared and the button is treated like a custom button.
Do I always have to manually code the back button?
The default back button (the one that takes the name of the previous controller) cannot be customized. But you can hide it and replace that button with a new one.
If you have a controller, you can do that on the viewDidLoad method. Overriding this method you are sure that all the elements have been set.
// allows you to hide the back button
NavigationItem.SetHidesBackButton(true,true);
// allows you to create a new customized button
NavigationItem.LeftBarButtonItem = new UIBarButtonItem(...);
UIBarButtonItem takes an handler that you can use to control the navigation.
In the handler you can do this:
NavigationController. PopViewControllerAnimated(true);

how to get done in button like in favorites tab of iphone phone app

how can i get done button like in favorites tab of native phone app which appears after we click edit button in favorites tab .
i though they were two barbutton items , hiding according to need ,but when i tried to place new barbutton item on my old one , old one replacing and new one only existing :S
thanks in advance
regards
If you have a UITableViewController UIViewController subclass, this is provided for you as the editButtonItem property. You can set either bar button item of your navigation bar to that and you will get the behavior out of the box.
[self.navigationItem setRightBarButtonItem:self.editButtonItem];
You may need to provide a setEditing:animated: implementation to transition your views to and from the editing state if you're not using a UITableViewController.