how to add View controller to every item on a menu? - drop-down-menu

i'm sure it's pretty easy , but i'm spending hours to get it right .
I download this simple menu from github :
https://github.com/Antondomashnev/ADDropDownMenuView
the view controller looks like this :
- (void)addDropDownMenu{
ADDropDownMenuView *dropDownMenuView = [[ADDropDownMenuView alloc] initAtOrigin:CGPointMake(0, 20)
withItemsViews:#[[self dropDownItemWithTitle:NSLocalizedString(#"Item 1", #"")],
[self dropDownItemWithTitle:NSLocalizedString(#"Item 2", #"")],
[self dropDownItemWithTitle:NSLocalizedString(#"Item 3", #"")]]];
dropDownMenuView.delegate = self;
dropDownMenuView.separatorColor = [UIColor blackColor];
[self.view addSubview: dropDownMenuView];}
i'm adding 2 more view controllers to the storyboard.
how can I connect them with Item 2 and Item 3 ? ( so I will see a different View every time i'll click different item ) .
any help will be appreciated...

I've changed demo code to show you solution.
In ViewController.m file add private category with two properties
#interface ViewController ()<ADDropDownMenuDelegate>
#property (nonatomic, strong) NSArray *viewControllers;
#property (nonatomic, strong) ADDropDownMenuView *dropDownMenuView;
#end
Then you need to create set viewControllers array. For example i want to create two instances of ViewController2 class (clean UIViewController template) in viewDidLoad
ViewController2 *vc1 = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewController2Blue"];
[self addChildViewController:vc1];
ViewController2 *vc2 = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewController2Yellow"];
[self addChildViewController:vc2];
self.viewControllers = #[vc1, vc2];
You have to add these view controllers as a child view controller to self. Don't forget about setting storyboardID for your view controllers in storyboard. In this case first view controller has ViewController2Blue storyboardID and second has ViewController2Yellow.
And the last thing to do is implementing ADDropDownMenuDelegate method
- (void)ADDropDownMenu:(ADDropDownMenuView *)view didSelectItem:(ADDropDownMenuItemView *)item
{
NSLog(#"%# selected", item.titleLabel.text);
for(UIViewController *viewController in self.viewControllers){
[viewController.view removeFromSuperview];
}
if([item.titleLabel.text isEqualToString:NSLocalizedString(#"Item 1", #"")]){
[self.view insertSubview:((ViewController2 *)self.viewControllers[0]).view belowSubview:self.dropDownMenuView];
}
else{
[self.view insertSubview:((ViewController2 *)self.viewControllers[1]).view belowSubview:self.dropDownMenuView];
}
}
In this method you remove all UIViewController's views from superview. And then according ADDropDownMenuItemView titleLabel text you add viewController's view to self view.

Related

Why is NSViewController not binding representedObject?

In short: I bind an NSTextField to the File's Owner (the view controller) and Model Key Path of representedObject.firstName, but editing the text field does not change the firstName.
Here are more details. I have a simple program that does nothing but create an instance of Thing (a simple class with some properties), and ThingViewController. The controller has an associated .xib with a simple UI -- a couple text fields to bind to properties of the Thing.
#interface Thing : NSObject
#property (nonatomic, strong) NSString *firstName;
#property (nonatomic, strong) NSString *lastName;
#property (nonatomic) BOOL someBool;
#end
And in the app delegate...
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSView *cv = self.window.contentView;
ThingViewController *vc = [[ThingViewController alloc]
initWithNibName:#"ThingViewController" bundle:nil];
theThing = [Thing new];
theThing.firstName = #"Rob";
vc.representedObject = theThing;
[cv addSubview:vc.view];
}
The ThingViewController.xib is simple:
And here is the binding for that first text field:
When I run, the text field does show "Rob", so it works in that direction, but then as I edit the text field, the firstName property of theThing does not change.
What am I doing wrong?
Edit: Here's a link to a zipped project file for the above code: https://drive.google.com/file/d/0B2NHW8y0ZrBwWjNzbGszaDQzQ1U/edit?usp=sharing
Nothing is strongly referencing your view controller (ThingViewController), other than the local variable in -applicationDidFinishLaunching:. Once that goes out of scope, the view controller is released and dealloc'ed. The view itself is still around, since it is a subview of your window's contentView.
Once your view controller is released/gone, the text field has no connection back to the Thing object so it is in effect calling [nil setValue:#"New first name" forKeyPath:#"representedObject.firstName"].
Add a strong reference to your view controller (e.g., an instance variable of your app delegate) and try it again.
#implementation AppDelegate {
Thing *theThing;
ThingViewController *vc;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSView *cv = self.window.contentView;
vc = [[ThingViewController alloc] initWithNibName:#"ThingViewController" bundle:nil];
theThing = [Thing new];
theThing.firstName = #"Rob";
vc.representedObject = theThing;
[cv addSubview:vc.view];
}

xCode: Activating an IBAction from within a dynamically added subview

I have a separate UIView class that constructs a simple footer bar that contains a UIButton.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:CGRectMake(0, 410, 320, 50)];
if (self) {
int buttonHeight = self.frame.size.height;
int buttonWidth = 70;
int nextBtnPosX =0;
int nextBtnPosY =0;
self.backgroundColor =[UIColor colorWithRed:254.0/255.0 green:193.0/255.0 blue:32.0/255.0 alpha:1.0];
[self sendSubviewToBack:self];
UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[nextBtn setTitle:#"Next" forState:UIControlStateNormal];
nextBtn.frame = CGRectMake(nextBtnPosX, nextBtnPosY, buttonWidth, buttonHeight);
[nextBtn addTarget:self.superview action:#selector(GoToNextPage) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:nextBtn];
}
return self;
}
I have several ViewControllers that then add this footer view class above to each view as a subview.
UIView *newFooter = [[theFooter alloc] init];
[self.view addSubview:newFooter];
Now the actual UIButton within the footer view needs to have its taget different for each viewController it is added to.
So I though it would be best to add the IBAction to the actual view controller then call this via the footer view.
But this is where I have come into a problem. How do I call the parent Controller to init the IBAction(GoToNextPage) from within the footer subview, from the addTarget?
Also would it be easier to have it all within the footer subview and pass in the target required, if so how else would this be done.
Here is a rough overview on what you should do.
This is how your UIView's header file should look
#protocol myViewControllerDelegate <NSObject>
#optional
- (void)didPushButton:(id)sender;
#end
#interface UIViewController : UITableViewController
{
__unsafe_unretained id <myViewControllerDelegate> delegate;
}
#property (nonatomic, assign) id <myViewControllerDelegate> delegate;
#end
Remember to #synthesize delegate; in your main file.
Finally in your main file, you will have an IBAction that receives the UIButton action.
Let's say that action is named buttonPushed.
Set that action to:
- (IBAction)buttonPushed:(id)sender
{
if (delegate)
[delegate didPushButton:sender];
}
Finally remember, that you need to set the delegate to each viewController you are using this viewController.
UIView *newFooter = [[theFooter alloc] init];
[self.view addSubview:newFooter];
newFooter.delegate = self;

How to use Storyboard to make popover that can be used in code?

I'm building a collection of forms each of which contains several fields. Some of the fields are UITextFields that will display a date. I've created a new class called DatePickerTextField, a descendant of UITextField. When a DatePickerTextField is tapped I'd like for a UIDatePicker control to appear in a popover.
My question is how do I use the storyboard to implement the popover? I can do a segue when there is a specific, visible control in the scene. But how do I represent a generic popover in the scene that I can attach to any instantiated DatePickerTextField that becomes active?
You can create segue that is not connected to any control but I don't think that there would be way to specify anchor point for popover from code. Another option is to create ViewController that is not connected with any segue. When editing storyboard, create ViewController which will be placed in popover, select it and navigate to Utilities pane->Attributes Inspector. Set Size to Freeform, Status Bar to None, specify unique Identifier that will be used to instantiate ViewController from code. Now you can change the size of ViewController by selecting its View and navigating to Utilities pane->Size Inspector.
After that you can create popover from code:
- (IBAction)buttonPressed:(id)sender {
UIView *anchor = sender;
UIViewController *viewControllerForPopover =
[self.storyboard instantiateViewControllerWithIdentifier:#"yourIdentifier"];
popover = [[UIPopoverController alloc]
initWithContentViewController:viewControllerForPopover];
[popover presentPopoverFromRect:anchor.frame
inView:anchor.superview
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
One caveat is that you need to hold reference to popover as ivar of your class, otherwise it'll crash because UIPopoverController would be released and deallocated after buttonPressed returns:
#interface MyViewController : UIViewController {
// ...
UIPopoverController *popover;
// ...
}
So, I had a similar issue, and in case others might benefit, I figured I'd share it, since I benefit so much from stackoverflow.
This solution allows you to set the anchor of a customizable popover segue. It also allows you to configure the segue to be modal or not (I could not find a way to prevent the segue by dimming the exterior context, so if someone knows how to do that, I would be interested in hearing it); this is accomplished by setting the passthrough views for the popover controller. I also added the capacity to specify a custom view, rather than the view of the source viewcontroller (since I needed this capacity); this portion is not critical to the solution.
DynamicPopoverSegue.h
#interface DynamicPopoverSegue : UIStoryboardPopoverSegue
#property BOOL isModal;
#property UIView* sourceView;
#property CGRect anchor;
#end
DynamicPopoverSegue.m
#implementation DynamicPopoverSegue
- (void)perform
{
if (!self.popoverController.popoverVisible)
{
UIViewController* dst = (UIViewController*)self.destinationViewController;
UIViewController* src = (UIViewController*)self.sourceViewController;
UIView* inView = _sourceView ? _sourceView : src.view;
self.popoverController.contentViewController = dst;
if (!_isModal)
{
[self.popoverController setPassthroughViews:[[NSArray alloc] initWithObjects:inView, nil]];
}
[self.popoverController presentPopoverFromRect:_anchor
inView:inView
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
}
#end
Then you just set your segue to "Custom" in the storyboard, and set the segue class to "DynamicPopoverSegue". In my case, since I wanted to associate it with dynamic layers in a view, I could not set the anchor, so I created the segue by control clicking from the view controller icon in the bar beneath my view controller to the view controller I was using to present the popupover.
To call the popover segue:
[self performSegueWithIdentifier:#"MyPopoverSegue" sender:self];
And to configure the popover segue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"MyPopoverSegue"])
{
DynamicPopoverSegue* popoverSegue = (DynamicPopoverSegue*)segue;
// set the anchor to wherever you want it to be
popoverSegue.anchor = _destinationFrame;
}
}
- (IBAction)pressItemChooseOprateRoom:(id)sender {
if (isPad){
// UIView *anchor = sender;
UIViewController *viewControllerForPopover =
[self.storyboard instantiateViewControllerWithIdentifier:#"OperateRoomList"];
_myPopover = [[UIPopoverController alloc]
initWithContentViewController:viewControllerForPopover];
CGRect rc=[self getBarItemRc:sender];
[_myPopover presentPopoverFromRect:rc
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[MLControl shared].popover =self;
// [self perfformSegueWithIdentifier:SEGUE_POP_OPERATEROOM sender:self];
}else{
[self iphoneOpenOperateRoomList];
/* [self performSegueWithIdentifier:#"iPhonePushOperateRoom" sender:self];
*/
}
}
-(void)iphoneOpenOperateRoomList{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"OperateRoomList"];
// if (!index.showTabBar) {
// vc.hidesBottomBarWhenPushed = YES;
// }
[self.navigationController pushViewController:vc animated:YES];
}
Just used the answer from Jonnywho for my SWIFT project. In case you need it:
Here's the SWIFT version:
let anchor: UIView = sender
var viewControllerForPopover = self.storyboard?.instantiateViewControllerWithIdentifier("GameAboutViewController") as! UIViewController?
let popover = UIPopoverController(contentViewController: viewControllerForPopover!)
popover.presentPopoverFromRect(anchor.frame, inView: anchor, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
Add a UIView in the scene dock.
You can add it as a subview to any existing view on the view controller.
You can then toggle it's isHidden property as you require.
You can add multiple such subviews and create many such popups.
This technique will save you from setting up a new View Controller and using segues.

How do I change a UIButtons label, label color etc. from another view controller?

In one view (main view) controller I have a UIButton that creates a new UIButton. When you make a long press on the new UIButton a new view controller (second view) is presented by presentmodalviewcontroller. In the second view I have a UITableView and in the first UITableViewCell there's a UITextField. What I want is that when you input something in the UITextField the new UIButtons title changes to that.
What I have done is to create a NSString in my app delegate. In the second view in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
I have used this code:
// Passing the TextFieldText to NewButton.
iAppAppDelegate *AppDelegate = (iAppAppDelegate *)[[UIApplication sharedApplication] delegate];
AppDelegate.ButtonText = [TextFieldText text];
iAppView = [[iAppViewController alloc] initWithNibName:#"iAppViewController" bundle:nil];
[TextFieldText addTarget:iAppView action:#selector(ApplyAllObjectsSettings) forControlEvents:UIControlEventEditingChanged];
In the main view I used this action to change the new UIButtons title:
- (void)ApplyAllObjectsSettings {
iAppAppDelegate *AppDelegate = (iAppAppDelegate *)[[UIApplication sharedApplication] delegate];
[NewButton setTitle:AppDelegate.ButtonText forState:UIControlStateNormal]; }
It does however not work. Any idea of how to make this work or another way to do it, would really be appreciated :)
Thanks in advance :)
Update:
In my second views .h file I have referred to the first view controller like this:
#class iAppViewController;
#interface ButtonSettings : UIViewController < UIPickerViewDataSource, UIPickerViewDelegate > {
// iAppViewController
iAppViewController *iAppView;
in the .m file I have imported the first view controller
#import "iAppViewController.h"
And this is the code I use to call the action:
[TextFieldText addTarget:iAppView action:#selector(ApplyAllObjectsSettings:) forControlEvents:UIControlEventEditingChanged];
And this is the action in the first view controller:
- (void)ApplyAllObjectsSettings:(id)sender {
[NewButton setTitle:((UITextField *)sender).text forState:UIControlStateNormal];
}
Your code isn't working because you never actually pass the new text to the button. When you say
AppDelegate.ButtonText = [TextFieldText text];
is set's the ButtonText (which I guess is an NSString) to the UITextFields current text, which is most likely empty. If you look at the docs or other examples, you will see that the selector for UIControlEventEditingChanged actually accepts one argument. This is the sender, so in your case, the UITextField which fires the event. Using it, you can access the entered text and you won't even need your ButtonText variable.
So change row where you set UITextViews target to this: (Notice the new ':')
[TextFieldText addTarget:iAppView action:#selector(ApplyAllObjectsSettings:) forControlEvents:UIControlEventEditingChanged];
Then change your listener to this
- (void)ApplyAllObjectsSettings:(id)sender {
[NewButton setTitle:((UITextField *)sender).text forState:UIControlStateNormal];
}
Update:
So as I said, the iAppView in your second view needs to point to the first view. One way of archiving this is by making it a property like this (in your ButtonSettings.h):
#interface ButtonSettings : UIViewController < UIPickerViewDataSource, UIPickerViewDelegate > {
...
iAppViewController *iAppView;
}
#property (nonatomic, strong) iAppViewController * iAppView;
And don't forget #synthesize iAppView; in your ButtonSettings.m file. Now, when you are creating the instance of ButtonSettings in your FIRST view, you can just pass the reference like this:
myButtonSettingsInstance.iAppView = self;

IOS: use navigation controller in a subview

In a view of my program I have a button and with this button I open a subview; this subview is a view with a tableview. I want go to another view when I push a row of the tableview so I want to make this with a navigation controller; how can I do this?
Your table view delegate will receive a tableView:didSelectRowAtIndexPath: message when you click on one of the table's rows.
You can put there your code to create the UINavigationController and push on to it your new view.
This sample code (from another answer of mine on S.O.) shows how you can do that:
UINavigationController* navigation = [[UINavigationController alloc] init];
iVkViewController *overviewViewController = [[iVkViewController alloc] init];
overviewViewController.title = #"First";
[navigation pushViewController:overviewViewController animated:NO];
This should help you getting things on track.
One side note: you might think of having a navigation controller from the very start, this would make your UI more "well-behaved", but this depends ultimately on your app requirements.
If UIViewController addSubView a view of UITableViewController,you want to push in the method of tableView:didSelectRowAtIndexPath,you should check if self.navigationController is nil. If it's nil,You probably should use
[self.parentViewController.navigationController pushViewController:controller animated:YES];
if self.parentViewController is also nil,Sometimes,you have to set a #property to point out the parentViewController In UITableViewController,like:
#property (nonatomic, weak) UIViewController *parentVC;
and in UIViewController:
UITableViewController *tableViewVC = [[UITableViewController alloc] init];
tableViewVC.parentVC = self;
In UITableViewController ,-tableView:didSelectRowAtIndexPath:
[self.parentVC.navigationController pushViewController:controller animated:YES];

Resources