Add UIView at a UIButton - iphone

Here is the requirement of my application:
There is a calendar type view which will show Date, Day, Total work hours of that day. What I need, when I click anywhere in that particular square block then it redirects my page to a new view.
I am thinking to should add multiple button and UIView on that button.
That can solve my problem but it is little bit problematic. How should I to do this?

Please look at UITapGestureRecognizer for your view. I think this is what you're looking for.
Here is a link to a tutorial on using UITapGestureRecognizer

Create a subclass of UIControl and a XIB with a UIView in it. Set the class type for the UIView to your new UIControl (not the file owner but the UIView). Then in the UIControl in the initWithCoder: method create the display component and add to view. Then set up you properties that you need to populate those controls.
To use this new control place a UIView where you want it on your screen and size it. Change its class type (in the inspector) to your new subclass. Now you can add outlets and actions to this view just like you would a UIButton. The only downfall is can't set the display controls from the inspector, you'll have to do that in the program. But the good side is you get a similar interface to a button and you can make it look anyway you like.
Hope this helps.

Related

How To Prevent Interface Builder From Adding Items To A Custom View?

UPDATE: Edited for clarity.
This is not a showstopper, but it is annoying, and I'd like to figure out how to address it.
I have a custom UIView. A UIScrollView, to be precise, that is programmatically populated with a bunch of UIViews at runtime. It is not meant to have anything embedded in Interface Builder (IB).
What happens with any UIView, is that when you drag another element over it, the UIView becomes "droppable," and allows the other element to be dropped into it.
There are a few native Apple elements that won't let you drop stuff into them, like UIPickerView.
Is there a flag or something I can set, so that it won't allow IB to add anything to it?
Like I said, not a showstopper. If stuff gets added, it is destroyed before the programmatic population happens, but it just seems "neater" to make it clear that it's not "droppable."
UPDATE: In the screengrabs below, the Login Picker View is a standard UIPickerView, and the Display Results Scroller View is my custom scroller. If I drag something out of the Library, and try to drop it on the Picker View, nothing happens. However, I am able to drop it into my Display Results View.
What I want, is to be able to declare my Display Results View as a "no fly zone," so it is no longer droppable.
I hope that makes it clear.
Cannot Drop onto A PickerView
Can Drop On My Custom View
I'm not saying this is a great solution, but if you're using a storyboard (not a xib) then it seems to work…
In your storyboard, instead of dragging a scroll view out of the library, drag out a container view.
IB automatically creates a new storyboard scene connected to the new container view by an embed segue. Delete the newly-created scene.
Set the container view's custom class to UIScrollView.
IB does not allow you to add any subviews to a container view.
Note that IB also doesn't show you any UIScrollView-specific properties in the Attributes inspector, so if you need to customize this funky scroll view, you have to do it programmatically.
The library doesn't offer the container view in a xib, hence the storyboard requirement.

iOS Custom Control

I am building a custom control that looks like the one in the image below.
It will basically be a menu with a slider. The arrows will allow me to change the three days show on the slider track. Acoording to the day I select with the slider I want to change some views on the main screen (this menu will on the bottom of my page). So basically this is the only thing that I will "listen" in my main controller: if some day has been selected.
I have figured out all the code I will have to write, but I am not sure wether I should subclass UIControl or UIView. And if so, where should I write the code of my controller (changing the days, adding the drag effec, etc ) in the UIControl (UIView)? Or should I subclass UIViewController, and write there all the code there. (but if so, why should I subclass UIControl (UIView) in the first way).
So basically I want to know what extra files I need to create, besides the view interface of my custom control(which I did in the IB), where should I put the code (IBOutlets, IBAction methods) and how do I communicate with the main view controller (I set the main controller as a delegate of my custom control?).
Sorry for the long post.
Thanks
I recommend to subclass UIControl. Users of this control can do [yourControl addTarget:self action:#selector(someMethod:) forControlEvents:UIControlValueChanged]; to react to changed values. In your control, when you have determined that a new day has been selected, you call [self sendActionsForControlsEvents:UIControlValueChanged]; and voila, all interested classes will get informed.
Keep that control as self-contained as possible. This means, only give it as much logic as you need to, and nothing more. Think about how you use Apple provided UI elements: try to make yours as generic (if practical; use common sense here). In short: you should thrive to make this control generic enough that it could be useful to you in other projects or other places of your app.
The short answer is you should subclass UIControl and put all of the logic to draw the component and interact with the component there. UIControl inherits from UIView and adds target/action behavior. This way you can sendAction:to:forEvents: with UIControlEventValueChanged whenever the date changes.
You could alternatively implement a delegate protocol for when the user changes the selected date.
For example:
#protocol DateSliderDelegate <NSObject>
- (void)dateSlider:(id)slider dateDidChange:(NSDate *)date fromDate:(NSDate *)oldDate;
#end
You don't want to use a UIViewController since it's job is to manage higher-level views, like the screen that your widget is on. You'll use a view controller later when you are consuming your component and do things like set the date to display initially and listen for change events.

Resizing and rearranging component controls using code/ IB

Is it possible to resize and rearrange UI components (say segmented controls, buttons, labels, etc) when these are created using IB?
I need to rearrange/resize few UI components on a button click on iPad screen.
Any idea?
It is possible.
You will need to create IBOutlets for the UI components you are interested in, then you can adjust the properties you are interested in (UIView's frame property)
IBOutlet's are created in the *.h file for you View controller.
IBOutlet UIView * view;
Then you can control click and drag from the file owner (in xib) to the UI element. (or right click and drag)
Yes it is. Implement the viewDidLoad method in your controller and set the frame to whatever you wish for your views and controls so long as you've attached them to an outlet.
Example:
self.myCustomView.frame = CGRectMake(x,y,width,height);
You may also access them by their tag.
If you assign them unique tag numbers in IB, then in your code, you can search within subviews of your view and find them by viewWithTag method.
You can set the initial position of the components in IB, hook up the elements with IBOutlets and on the button click move them in code. I don't think you can rearrange them in IB.
For a cool effect you can use [UIView animateWithDuration:animations:] to move whatever components you need

Updatable, custom view on a UIToolbar

I want to make a small area to present some information in the middle of a UIToolbar and was wondering what the best way to do this is.
I need to show some text and a graphic, both of which need to be updated (around every 3 seconds) as new information arrives. The graphic is similar to the iPhone signal strength indicator, so it can be either custom drawn or selected from one of 3 graphics (low, medium, high strength).
I'll probably use initWithCustomView: to create a UIBarButtonItem, although I would like the view to be clickable (to allow the user to change the information shown).
What's the best way to make that view? Can I use a NIB, or do I need to do custom drawing in the view? What's the best way to update the buttons? I'm assuming that I'll have to remake the toolbarItems array each time and set it when the information changes. Is there a cleaner way to do this? Thanks.
Using initWithCustomView: sounds like a good way to go. You can create your custom view any way you want: with a NIB, by drawing it, even using images. It can also have its own subviews. It can be any object that inherits from UIView. (So, if you wanted, you could even make it actionable by using a UIButton, a custom UIControl, or a custom UIView with a gesture recognizer attached.)
You shouldn't have to remake toolbarItems (or, for that matter, do anything with it after you've added all your button items) if you just keep a pointer to your custom view UIBarButtonItem. It could be an instance variable. Then, to update the custom view, you could access it as you would any other view. I've never actually tried this, but I can't see any problem with doing it.
You sound like you had it mostly figured out. Hope this is helpful.
I needed the same solution and was hoping for some code examples from you. So I ended up doing it all in IB and the steps are here as follows:
Create UItoolbar in IB with no Items. (A Bar Button Item will be added again once you add the UIView)
Add UIView as subview of UIToolbar
Add UILabels to subview of UIView that is already a subview of the UIToolbar.
Create IBOutlets from UIToolbar, UIView and each UILabel and then you can reference the labels in your app.
I did set the backgrounds to clearColor so the text appears on top of UIToolbar without any box or borders.
And the text updates dynamically which was the desired outcome.
Hope this helps someone as this has been eluding me for a while.

Floating a UILabel above OpenFlow

How do you get a UILabel to float above Alex Fajkowski's implementation of CoverFlow called OpenFlow?
Ok I've figured it out. I just had to use bringSubviewToFront with the UILabel.
Thanks to everybody who answered.
Make an OpenFlow instance and a UILabel instance subviews of a parent view, placing the label atop with flow view using -insertSubview:atIndex:, -addSubview: or similar e.g.:
[self.view addSubview:myOpenFlow];
[self.view addSubview:myLabel];
To answer the second question, try this: edit the AFOpenFlowView in IB and add the label view as a subview of that wherever you want it to appear, then set the attributes on the view to Hidden if you don't want it to appear by default. Create an IBOutlet for the label view in your controller so you can easily manipulate it, such as setting hidden to show in order to show it. I'm not familiar with open flow so I'm not sure if it will programmatically create a subview that will obscure your label. If so you may need to use the outlet to move it to the front using UIView methods.
The Openflow example code shows a perfect demonstration of this. Alex adds an info button in the top right of the openflow view using interface builder. This is exactly what you are asking for.
I would add that if you can't understand what is going on from the example code, you should look at more code examples from Apple demonstrating simple UIView usage.
It's not so easy because the label name has to change for each picture. We have to work with index, and with event.. move.. tap.. selected...I'm working on too !