How to Show popupViews - ios

I have UiView,In my Uiview I have one Button And one textField.
When I click the button i need to show the popUp for date values.
when i click the textfield,other popUp with Names.If user select any name from there,that name will show in textfield.If User click on Button,Dates will be arranged by Ascending or Descending order.
Probelm is,My Uiview contain one Main table also.
Now how to differentiate that main table and popUp views.
Thanks.

Create your other popups as separate classes, and add them to your view when needed.
Example:
Create a class called "Names" which inherits from UIView or UIViewController
Create a class called "Dates" which inherits from UIView or UIViewController
To present any of those classes as a popup, you have 2 options:
In case you used UIView, then you can present them with [self addsubview:nview] & [UIView animationwithduration] to make the popup animation.
In case you used UIViewController, just use [self presentViewController:nview].

Each view will have tags, use them.
tableView.tag = 1;
textBoxView.tag = 2;
alertView.tag = 3;
Now that you have the tags you can differentiate the views with respect to the tags.

Related

ios-Update multiple UIViews on UIScrollview

I have a ViewController that contain a UIScrollView, that contains a few views which are custom UIViews (very simple uiimage + uibutton) that the user can scroll between (one custom view at the a time).
I want the user to be able to "mark" a photo, and then to display a certain text, when the user select a different photo (by using the button), I want to update the text on the previous selected photo and the current selected.
What should I do?
Inside the view itself I don't have access to the previous view, I figured I should send a notification to the view controller but than I have no access to the button I want to update
You could use tags to identify the scrollViews subviews.
Add a tag to the subview and the same tag to the button that belongs to the subview while adding them to the scrollView.
view.tag = 1;
button.tag = 1;
Then in the method called by the buttons, you can use the buttons tag to get the according custom view.
-(void)buttonClick:(id)sender{
UIButton *btn = (UIButton*)sender;
NSInteger tag = btn.tag;
UIView* customView = [self.scrollView viewWithTag:tag];
}
To access the prevoius selected view, store the tag of that view when editing for the next call of the function.

Get a UIButton's tag from the UIButtons which are in the popover presented by this UIButton

The headline seems lengthy but what I'm trying to do is quite simple.
I have a couple of identical buttons lined in a row and set their tags to 0...n. Clicking on any of them (the 2nd for example) would bring up a popover view in which there are several buttons representing different options (A, B, C, D). What I want to do is to turn the 2nd Button's title to B if we click on option B.
The problem is that the popover view does not know which Button presented it, since all popoverViews are instances of the same UIViewController class. So I am thinking of distinguishing the n buttons by setting their tags to different values. However, I don't know how to get the UIButton's tag from a button inside the popover this UIButton presented.
Many thanks in advance!
This is how I will solve this in swift. I will declare a delegate in the popoverViewController and have a method e.g
protocol popOverViewControllerDelegate: class {
func popOverViewController(controller: PopOverViewController,
didSelectItem buttonTitle: String)
}
then I will add a target action to the UIButton in the popOver
button.addTarget(self, action: "selected:",forControlEvents:.TouchUpInside)
the select method will have sender passed to it
func selected(sender:UIButton){
delegate?.popOverViewController(self, didSelectItem: sender.currentTitle)
//dismiss viewcontroller
}
remember to declare
weak var delegate: popOverViewControllerDelegate!
now have the viewcontroller that called the popOver, subclass to this delegate and implement it's method. Whenever a button in the popOver is selected, this method will be called and the currentTitle of the method will be passed. You can now use that to set the currentTitle of the button that called the popOver.
In case you need further help with the last part, please ask.
I've fixed the problem by adding a property tagNumberso that after instantiating the popoverViewController's class, I set the instance's tagNumber to the sender's tag. Then I send the tagNumber back together with sender.currentTitle. This way, the presenter of the popover could know both the title and tag number of the UIButton.

Tab bar with multiple tabs using only one view controller

I was not able to find an answer to my problem on SO, please link me if an answer already exists somewhere! My problem seems pretty common- i have a tableview embedded in a nav controller and i want a tab bar with 3 items, where the 3 items change the tableview to populate different data, the first tab representing the default data in the tableview when the view is loaded.
But it seems unnecessary to make two extra UITableViewController classes for the other two tabs, when i can simply just change the data populating the table, with a simple [tableView reloadData]. How do you go about doing this, and also how to implement this in storyboard?
Sounds to me what you really want is a single UITableViewController with a Segmented Control to change the view.Tab Bars are for navigation.
Segmented Control
Use UIControlEventValueChanged to detect the change, load the new data and reload the view.
You didn't indicate what you mean by 'different data'. If you mean something like perform some action which updates data via a CoreData fetch/predicate/sort, etc. then using the Segmented Control is probably the way to go. If your data is dramatically different (different entities, different data management, etc. ) then you should probably go with separate UITableViewControllers as you should not try to over generalize your UITableViewController but rather pair your data to your UITableViewController. After all, that's what it is for.
You can make multiple relationship segue to one controller with ctrl+drag like this.
storyboard> 5 times ctrl+drag to one Navigation Controller
And you should make CustomTabBarController.swift to modify tabs.
Don't forget to change class name of TabBarController that is drawn in storyboard.
class CustomTabBarController: UITabBarController {
let MENUS = ["tab1", "tab2", "tab3", "tab4", "tab5"]
override func viewDidLoad() {
super.viewDidLoad()
let items = tabBar.items!
for (var idx=0; idx<items.count; idx++) {
items[idx].title = MENUS[idx]
items[idx].tag = idx
}
}
...
}
You can use tag or selected index of tabs on the ViewController.swift
let tag = self.tabBarController?.tabBar.selectedItem!.tag
let selectedIndex = self.tabController?.selectedIndex
What you can do is reuse the same Class and have a check in you viewDidLoad method to determine how to populate your tableView, for example:
- (void)viewDidLoad{
//itemType is a property you set
//when instantiating the controller
int index = self.itemType;
switch (index) {
case 0:
[self populateTab1];
break;
case 1:
[self populateTab2];
break;
case 2:
[self populateTab3];
break;
default:
break;
}
}
In storyboard, add UITabbar into your table view controller. Do not use UITabbarViewController which works as a container of view controllers. On the other hand UITabbar is just a control unit.
Set the delegate of the tab bar to your table view controller, and add the following protocol method:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;
You can load the correct data in this method. You can get the index of the selected item (button) by
NSUInteger index = [tabBar.items indexOfObject:item];

Should i create a view ( consisting UIButton, UILabel etc) in a separate UIView class or inside UIViewController?

I have a UIViewController say viewControllerA which contains some view element like UIButton, UILabel etc. Now my question is should I create those view elements in a separate UIView class and then add in UIViewController, or should I create those view elements directly inside the UIViewController. Accordingly to MVC isn't it appropriate to create view elements inside a separate UIView class and then add this in UIViewController?
The standard place to build the view hierarchy in a UIViewController is in the -viewDidLoad method. That method gets called whenever the UIViewController's view is created. The view controller's view will be loaded from the NIB/Storyboard if applicable; your outlets will be wired up; and then -viewDidLoad is called for you to perform further customization:
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0,0.0,100.0,40.0)];
[self.view addSubView:aLabel];
}
In Cocoa/Cocoa Touch you don't always want to subclass everything the way you would in, say, Java. There are often other preferred means of extending the functionality in built-in classes such as Objective-C categories, delegation, and pre-defined properties.
It's certainly possible to do this sort of thing another way, but this is the most "Cocoa-like" way to do it. Actually, the most "Cocoa-like" way would be to create the view hierarchy in Interface Builder, but if you want to do it programmatically this is the usual way.

how do I display different views in a view controller using a tab bar

I have a view controller that has a map view and a second view under the a tab bar. How do I go about updating the second view when I press buttons on the tab bar?
I tried:
LocationNotesViewController lnvc = new LocationNotesViewController();
lnvc.View.Frame = MainPageTabBarView.Frame;
MainPageTabBarView = lnvc.View;
Nothing happens...the view doesn't update.
I want to update the second view with different things when a user clicks on the tabbar...
If you put a UIView underlying whatever data you want displayed in it, you can use the IBAction of the tabBar to programtically cahnge out the contents of the UIView.
Or you could have the IBActions of the tabbar create new UIViews on the fly, containing whatever you want inside.
Code to do this would be like explained here: http://www.programmerscountry.com/creating-uiviewcontrols-programatically/.
Not exactly the same, but you will understand how it works from that answer.
To switch UIViewControllers use this piece of code:
UIViewController *viewController =
[self.storyboard instantiateViewControllerWithIdentifier:#"4"];
[self presentViewController:a animated:YES completion:nil];
I think this is a work around - but I was able to make this happen by doing:
LocationNotesViewController lnvc = new LocationNotesViewController();
lnvc.View.Frame = new RectangleF(MainPageTabBarView.Frame.X, MainPageTabBarView.Frame.Y - MainPageTabBarView.Frame.Y, MainPageTabBarView.Frame.Width, MainPageTabBarView.Frame.Height);
MainPageTabBarView.AddSubview(lnvc.View);

Resources