UIPicker specific question(iphone) - iphone

I am starting to learn to use the UIPicker in an application. what I want to do is that when the user clicks on the zipcode textbox, I want the uipicker to pop up and display list of zipcodes available. Just like a drop down in c# or VB.net. Do I have to create a new view and place the uipicker there?
Thanks

You need a view for placing a text box. It does not matter if it needs to be a new view or the old view - just place the text box in the view you require.
make sure that
textField.inputView = yourPickerView;

In order to have a UIPickerView appearing in your app you do not need an additional view.
Assuming you are in a UIViewController:
#interface MyController : UIViewController {
UIPickerView* mPicker;
}
-(void)showPicker;
-(void)hidePicker;
#end
-(void)showPicker {
[self.view addSubview:mPicker];
mPicker.center = CGPoint // set out of sight
[UIView beginAnimations:nil context:nil];
// do your transformations
[UIView commitAnimations];
}
-(void)hidePicker {
// do your exit animations
}
You can also add a delegate function to the second animations to remove the mPicker from the superview.
For more information have a look at the UIView Reference.

Related

Application Design - Menu system

I have a UIViewcontroller that has a UITableView and and a couple of buttons, (add and done)
When the user clicks add I need to get some details (which represent a notification they are setting in the app).
Details like notification name, a number, target score etc...
What is the best way to get this info, is it a good idea to use a UIView and animate it onto the screen with some textfields or something like that?
Any good cocoa controls you can recommend?
Problem is my app already transitions from the rootview controller to the notification input controller modally, I was trying to get a simple UI to just enter data, I could use UIAlertViews but they are so ugly....
based on your comment above:
CustomUIView *inputView = [UIView alloc]initWithFrame:CGRectMake(offscreenRect)];
inputView.alpha = 0;
self.notificationInputController.userInactionEnabled = NO;
[UIView animateWithDuration:.5
animations:^{
inputView.alpha = 1;
inputView.rect = CGRectMake(onScreenRect);
}
completion:^(BOOL finished) {
[self.notificationInputController addSubView:inputView];
self.notificationInputController.userInactionEnabled = YES;
}];

How to create simple menu?

I'm trying to create simple (facebook like) menu in my app. I've found several questions, most of them had accepted answer, but usually answer is to use some programs done by developers.
These programs are often in old version of xCode (one, that didn't use storyboards) and those, that were done in storyboard, were too complicated for me to implement in my app.
So I found one question, which had as an answer something like this:
1. Create your menu view controller (UITableViewController for me)
2. Hide this controller in your initial view controller :
MenuViewController *menView = [self.storyboard instantiateViewControllerWithIdentifier:#"Menu"];
[self.view sendSubviewToBack:menView.view];
3 Create a button (maybe pan gesture later), in it's method, do something like this:
-(IBAction)menuButtonPressed:(id)sender
{
CGRect destination = self.navigationController.view.frame;
if (destination.origin.x > 0) {
destination.origin.x = 0;
} else {
destination.origin.x +=254.5;
}
[UIView animateWithDuration:0.25 animations:^{
self.navigationController.view.frame = destination;
} completion:^(BOOL finished)
{
self.view.userInteractionEnabled = !(destination.origin.x > 0);
}];
}
What this does I guess, it's that it basically moves your view to the right by fixed length. I suppose, that your view, that you sendedToBack (your UITableView) should be exposed and interactable. However, all it does for me, is moving top view to the right, and leaving blank black-colored screen behind.
Now I think, that my problem is either bad instantiation of menuView, or just that I understanded this guide wrong.
If anyone knows, how to deal with this, I would be very thankful for an answer. Maybe there is some already done app, that is as easy as this to understand and hopefully easier to implement in my code :)
Thanks!
I would do this using container views in storyboard. You can size the left one how you like, then add a right one next to it, and in the inspector change its width to 320 -- most of it will go off the screen to the right, and it will resize its embedded controller to be full screen size. You can delete the view controller that you get with the left container view, then drag out a table view controller, and connect it from the left view with an embed segue (it will be the only choice when you control drag from the container view). I added two swipe gesture recognizers (one left and one right) to the main controller's view and connected them to the 2 methods in the main controller. Then in the main controller I have this code in the .h:
#interface ViewController : UIViewController
#property (weak,nonatomic) IBOutlet UIView *rightView;
#property (assign,nonatomic) CGRect rightRect;
#end
And in the .m, only this:
#implementation ViewController
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.rightRect = self.rightView.frame;
}
-(IBAction)revealSidebar:(id)sender {
[UIView animateWithDuration:.3 animations:^{
self.rightView.frame = self.view.window.frame;
}];
}
-(IBAction)hideSidebar:(id)sender {
[UIView animateWithDuration:.3 animations:^{
self.rightView.frame = self.rightRect;
}];
}
#end

Hide UIPickerView in UITableViewController while touch outside

Right now I have 2 different UIPickerView in side my UITableViewController. I only show them upon tapping of certain cells in the table. What I'm trying to do is to hide the pickers whenever I touch outside the pickers. Is there a delegate method or something similar to achieve this? I prefer to keep my controller as a UITableViewController instead of a simple UIViewController since I have a textView in one of the cells and scrolling after the keyboard shows is just a bit too much in a UIViewController.
Thanks in advance.
One of the Possible solutions is that when a particular cell is tapped and you handle picker (to present the picker), you can insert a view called as MASK View over the tableview. (with Frame as self.tableview.frame - yourPicker.frame.size.height ). Now when ever you get any click on this view you can handle it as follows
-(void)showMaskView{
if (!viewMaskView) {
CGRect viewRect = CGRectMake(0, 0, self.tableView.frame.size.width, self.tableView.frame.size.height - yourPicker.frame.size.height);
viewMaskView = [[MaskView alloc] initWithFrame:viewRect];
viewMaskView.delegate = self;
}
[self.view addSubview:viewMaskView];
[self.view bringSubviewToFront:viewMaskView];
}
-(void)removeMaskView{
if (viewMaskView) {
[viewMaskView removeFromSuperview];
}
//Remove the Picker
}
In the MaskView class you can handle the touch as follows
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if(self.delegate && [self.delegate respondsToSelector:#selector(removeMaskView)])
[self.delegate removeMaskView];
}
you can see the colored mask view over the Picker in the image. When tapped it removes picker.

Nested ViewControllers and their memory imprint? Improvements if this is very bad?

#Jonah pointed me to the following explanation he has provided:
Question on View Controller's view
I currently have a MainViewController whose main(root?) view contains an Add Record UIButton which is linked to the following method and is dependent on which page the user currently has open in a UIScrollView:
#pragma mark -
#pragma mark Add View Support
- (void)add:(id)sender {
MyAddViewController *controller = nil;
// Dynamically creating and allocating the Add View Controller
if (dataViewPageControl.currentPage == 0) {
controller = [[MyAddViewController alloc] initWithAddType:1];
} else {
controller = [[MyAddViewController alloc] initWithAddType:0];
}
controller.delegate = self;
// Slide the new AddViewController's view in from the right
CGPoint initialCenter = self.view.center;
controller.view.center = CGPointMake(initialCenter.x + 320, initialCenter.y);
[self.view addSubview:controller.view];
[UIView beginAnimations:#"animation" context:NULL];
[UIView setAnimationDuration:0.35];
[UIView setAnimationDelegate:controller];
controller.view.center = CGPointMake(initialCenter.x, initialCenter.y);
[UIView commitAnimations];
}
MainViewController is the delegate for MyAddViewController (which has a CANCEL/CLOSE button) and when it's close button is clicked the MyAddViewController is released through the close method in this MainViewController.
As you can see, MyAddViewController's view becomes a subview to the current root view (Bad? Bad? Bad?) MyAddViewController's view is as a FULL size view (320x416) but whose top half is transparent, so that MainViewController's view can be seen below on the top half and the bottom half contains the AddViewController's view and textfields etc....
Based on what I've read from #Jonah just now at the link at the very top this sort of design is BAD, BAD, BAD. I've noticed when testing in Instruments that MyAddViewController sometimes lags when sliding in. I'll click the ADD button to fire this method and there is sometimes a 3 - 5 sec delay before the view slides in. Some sort of delay in all the memory management and cleanup?
I'm assuming I can fix this by making a single MyAddViewController property inside MainViewController which will then prevent me from allocating and deallocating on every button click. That should make things smoother? My only worry is that this will cause a greater memory imprint at application load time. Instruments seems to show a 516KB allocation every time I click ADD for the first time in my app.
Any recommendations though on design and how to improve this? The reason I like having separate view controllers with nested views like this is because MyAddViewController has a lot of logic with Core Data and such and MainViewController is pretty busy enough already as it is. Trying to keep things modular.
Thanks in advance!
I think the best solution is to redesign your MyAddViewController to be a controller which inherits from NSObject rather than UIViewController. The MyAddViewController can then manage a subview of your MainViewController allowing you to keep your controller logic nicely encapsulated without abusing UIViewController.
I've tried to describe why nesting the views of multiple custom UIViewController classes is problematic here: http://blog.carbonfive.com/2011/03/09/abusing-uiviewcontrollers/
An add view controller is the perfect type of thing to use modal presentation for. Instead of handling all that animation yourself, just present it like this:
[self presentModalViewController:controller animated:YES];
Edit
Then I would recommend making MyAddViewController a property on MainViewController and write a custom accessor. Additionally I would defer setting it's addType (if possible) until the point at which you will actually animate the view in. This way we can just have one generic MyAddViewController hanging around, instead of having to create a new one every time the detailPageControl changes.
- (MyAddViewController *)addViewController
{
if (!addViewController)
addViewController = [[MyAddViewController alloc] init];
}
Then before you animate it...
self.addViewController.addType = 0;
if (dataViewPageControl.currentPage == 0) {
self.addViewController.addType = 1;
}

keyboard hiding textfields in tableViewController

I am using a tableViewController having 2 sections ....
1st section has 7 rows & 2nd section has 2 rows.
So when i edit in textfields of 2nd section keyboard hides these field so how i will handle
keyboard for these fields only.(i am using tableviewController which has default scrolling).
I am new to objective -C.....Your help please.
Thanks.
On tapping any cell with text field, you can navigate to another view with only a text field, keyboard and Done and Cancel buttons. this feels pretty neat. You can see this in many of apple's iphone apps as well... e.g. Go to iPhone Settings -> Mail, Contacts, Calendars -> Signature.
EDIT: since you cant use the standard way you can move the complete view up with following code:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
CGPoint currentCenter = [self.view center];
CGPoint newCenter = CGPointMake(currentCenter.x, currentCenter.y - 150);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[self.view setCenter:newCenter];
[UIView commitAnimations];
return YES;
}
change the animation duration and the change in position of the center (150) in this code according to your requirements. I have assumed that your view controller is the delegate for your textfield.
To bring the view back to it's original position, use same code but add to center.y in textFieldShouldReturn or textFieldShouldEndEditing etc
CGPoint newCenter = CGPointMake(currentCenter.x, currentCenter.y - 150);
P.S. : I'm still not sure you should be using this second approach.