i have a view which contains some text fields and picker views and one button,when clicked on the button all these textfields and picker view should appear again in the same view.Now how should i take the initial view and how to load the same view with the previous view when clicked on the button.
Thanks in advance..
Setting the Frames an Adding subview (text fields and picker views) on the button click , you can appear those subviews to the main view.
Or else you can initially put them all in the main view and initially hide them. And on the button click set them Hidden : NO.
Here is the approach:
On your View controller : Add a button and a UIScrollView.
Create an independent view say MyView containing all your TextFields and pickers. (you can create this via xib or via code)
In ViewDidLoad method of your controller add this MyView on UIScrollView.
On onButtonPressed similarly add MyView on UIScrollView.
If I got your problem then this code might help you. I am adding only text field on button click event. So I think it might give you idea for another views and you can do the same with another views. I am giving you .m file, hope you can write .h file. Here is my .m file looks like -
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize btn = _btn; // IBOutlet of UIButton
#synthesize txtFieldOne = _txtFieldOne; // IBOutlet of UITextField
#synthesize txtFieldTwo = _txtFieldTwo; // IBOutlet of UITextField
- (void)viewDidLoad
{
newYCoordinateForNewTxtFld = 40.0; // newYCoordinateForNewTxtFld is of type CGFloat
frame = self.txtFieldTwo.frame; // frame is of type CGRect
[super viewDidLoad];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return true;
}
// TextField Delegate Method you might required in your project
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (IBAction)btnClicked:(id)sender
{
// I am giving u a simple idea, so written simple code. You can make code more usable with ur view by adding various functions, loops, etc ,etc.
int tagValue = 0;
UITextField *newTextField = [[UITextField alloc]initWithFrame:CGRectMake(frame.origin.x, frame.origin.y+newYCoordinateForNewTxtFld, frame.size.width, frame.size.height)];
newTextField.delegate = self;
newTextField.tag = tagValue++;
frame = newTextField.frame;
newTextField.backgroundColor = [UIColor whiteColor];
[newTextField setBorderStyle:UITextBorderStyleRoundedRect];
[self.view addSubview:newTextField];
newTextField = nil;
newTextField = [[UITextField alloc]initWithFrame:CGRectMake(frame.origin.x, frame.origin.y+newYCoordinateForNewTxtFld, frame.size.width, frame.size.height)];
newTextField.tag = tagValue++;
newTextField.delegate = self;
frame = newTextField.frame;
newTextField.backgroundColor = [UIColor whiteColor];
[newTextField setBorderStyle:UITextBorderStyleRoundedRect];
[self.view addSubview:newTextField];
}
#end
Related
I have a simple question, how to connect a textfield to another Control view, if I click on the textfield, instead of show the keyboard it jump to another view
Thanks for answers
In the delegate method of the TextField, wich is textFieldShouldBeginEditing, add code that go from current View to another View.
Or you can use tap gasture recognizer on TextField to get the touch.
Here is the code:
create a TextFied in your IB and connect it to .h file
#interface ViewController : UIViewController<UITextFieldDelegate>
#property (strong, nonatomic) IBOutlet UITextField *firstTF;
And in .m file add this
#synthesize firstTF;
- (void)viewDidLoad
{
[super viewDidLoad];
firstTF.delegate= self;
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
secondViewController *ainfoController = [[secondViewController alloc] initWithNibName:#"secondViewController" bundle:nil];
[self presentModalViewController:ainfoController animated:YES];
return YES;
}
If you are adding the Textfield by code then,
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(x, y, width, height)];
textField.delegate = self;
[self.view addSubview:textField];
and add this method
- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField{
// you can add the code for present a new viewController here
return NO;
}
make sure you have added UITextFieldDelegate in your .h file
I really don't get in why you need this but as a developer we do firmly believe to implement all scenarios, so in your case, you can achieve the same by following the any one beneath mentioned tacts:
1) In this approach, you need to override the textfieldshouldbegin delegate and use the navigate code for moving from one screen to another and don't forget to call resignFirstResponder here.
2) While in this second approach what you can do, just overlap a custom button(with neither image nor any text) and just on his click event method write your navigation code to move another screen.
Do that stuff, you'll get what you want and in case still you find any difficulty just shout over me.
i'm trying to build a quiz that sets the value of a UILabel dynamically through code.
i've done this successfully before, but for some reason it's not working this time. i suspect it's because the structure of this app is different. i've tried different fixes but haven't been able to get it to work.
the way my app is set up, i have a view controller with a view that has a segmented control. when you press one of the switches on the segmented control, it inserts a subview like this:
menuTable.hidden = YES;
additionPracticeController *additionPractice = [[additionPracticeController alloc]
initWithNibName:#"additionPractice"
bundle:nil];
self.addPracticeController = additionPractice;
[self.view insertSubview:additionPractice.view atIndex:0];
[additionPractice release];
the view controller for that subview displays its view like this:
- (void)viewWillAppear:(BOOL)animated{
firstNumberString = [NSString stringWithFormat:#"%d",arc4random() % 10];
firstNumberLabel.text = firstNumberString;
secondNumberLabel.text = secondNumberString;
[super viewWillAppear:animated]}
my outlets are connected and i can get the values to appear by setting them statically from the nib (even though that's not what i want). i've tried to set firstNumberString equal to all sorts of values, but nothing shows up when i set the values through code.
i'd really appreciate it if someone could help me solve this problem.
It sounds like you have the label connected in Interface Builder. I would need to see more code to know exactly what you are doing wrong. Make sure you are using a property for your label. The below code is a simple example of how this works.
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
IBOutlet UILabel *_displayMessage;
}
#property (nonatomic, retain) UILabel *displayMessage;
#end
ViewController.m
#import "ViewController.h"
#implementation ViewController
#synthesize displayMessage = _displayMessage;
- (void)viewDidLoad
{
[super viewDidLoad];
self.displayMessage.text = #"Text Changed!";
}
- (void)viewDidUnload
{
self.displayMessage = nil;
[super viewDidUnload];
}
- (void)dealloc
{
[_displayMessage release];
[super dealloc];
}
#end
Instead of making your class a subclass of UIControl just implement this method below. When the user hits done or return the keypad will resign
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
To make the text field dismiss when the user taps outside of the text field.
Place this in ViewDidLoad:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
Place this method within the class:
-(void)dismissKeyboard
{
[aTextField resignFirstResponder];
}
Also if you want to dismiss the text field from another button spefically and not just a screen tap. Then just call this from within the button.
[your_textfield_name resignFirstResponder];
I have a ViewController with xib,.h,.m files. Now i want to display it as a popUpView in another viewController. so I did like this...
Second *view2 = [[Second alloc] initWithNibName:#"Second" bundle:nil];
[view2.view setFrame:CGRectMake(20, 20, 280, 400)];
{self.view addSubview:view2.view];
but the problem here I am able to set frame randomly, but the viewController contains a textField and button. They are not getting added at random positions on the view like some textField comes out of the frame of view added.
After some googling i find this, but this not enough to solve my issue.
Adding a ViewConroller's View as subview programmatically
How to solve it...
It seems that you are changing the frame of the view in your view controller but not handling the repositioning of the elements inside the view. What you can do is override the layoutSubviews method in the view so that when the frame changes the text fields reposition themselves.
You will have to create an actual UIView subclass which will layout the text fields,
#interface SecondView : UIView{
}
#implementation SecondView{
- (void) layoutSubviews{
//self.frame would be to most up-to-date frame that you just set
}
}
Your UIViewController will just create the view like so,
#interface Second : UIViewController{
}
#implementation Second{
- (void) loadView{
self.view = [[[SecondView alloc] init] autorelease];
}
}
I'm looking to create a sizeable subview that's draggable like this:
If there is an IBAction that takes you to the next View (SecondViewController) and when it does, there's another IBAction there and when you click on that one, it creates a SubView that's about half of the size of the current screen you're in (SecondViewController) that shows the third view controller that would be created? Also, how would you make that subView draggable? Thank you for helping.
Sorry, just to be clear, you want your second view controller to have a button that when tapped, adds your third view controller taking up half the screen at the bottom?
If this is the case then you can do this with the new view controller containers in iOS5.
Ok, so you have three view controllers. For the sake of this lets say your class are called FirstViewController, SecondViewController and ThirdViewController.
I assume from what you say that you already have you instance of FirstViewController with a button, that moves you on to an instance of SecondViewController, and that the issue is then getting SecondViewController to add an instance of ThirdViewController to the bottom half of the screen when a button is pressed.
The .m file for SecondViewController will need to do something like this:
#import "ThirdViewController.h"
#interface SecondViewController ()
#property (retain) ThirdViewController *thirdViewConroller;
- (void)buttonTap;
#end
#implementation SecondViewController
#synthesize thirdViewConroller = _thirdViewConroller;
- (void)dealloc {
self.thirdViewConroller = nil;
[super dealloc];
}
- (void)loadView {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.titleLabel.text = #"Show third controller";
[button addTarget:self action:#selector(buttonTap) forControlEvents:UIControlEventTouchUpInside];
button.frame = // Some CGRect of where you want the button to be
[self.view addSubview:button];
}
- (void)buttonTap {
// When the button is tapped, create an instance of your ThirdViewController and add it
self.thirdViewConroller = [[ThirdViewController alloc] initWithFrame:/* Some CGRect where you want the controller to be */ ];
[self.thirdViewConroller willMoveToParentViewController:self];
[self addChildViewController:self.thirdViewConroller];
[self.thirdViewConroller didMoveToParentViewController:self];
}
#end
This should give you a button on your second controller, that will create and add you third controller. Make sure yo us till have all the standard methods that you had before, this should be in addition to what you have.
In your interface for ThirdViewController:
#interface ThirdViewController : UIViewController <NSObject>
- (id)initWithFrame:(CGRect)frame;
#end
Then in the implementation of your ThirdViewController:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithNibName:nil bundle:nil];
if (self) {
self.view.frame = frame;
// Do your init stuff here
}
return self;
}
It should then handle adding the views and such forth.
Make sure your thirdViewController class has a valid initWithFrame: initialiser method.
This should do the trick, if you need any further help let me know :)
In my application, I am trying to remove all existing subviews and add a new one created in Interface Builder. However, I don't seem to be able to connect the view.
When a button clicks, the following function is executed (inside a subclass of UIViewController):
// Display a list of settings to choose from
- (void) settings
{
SettingsRootController *settingsController = [[SettingsRootController alloc] initWithNibName:#"SettingsRootController" bundle:nil];
_settingsController = settingsController;
for (UIView *view in self.view.subviews)
{
[view removeFromSuperview];
}
[self.view addSubview:_settingsController.view2];
int a = [self.view.subviews count];
[self.view setNeedsDisplay];
......
}
#import <UIKit/UIKit.h>
#interface SettingsRootController : UIViewController
{
IBOutlet UIView *_view2;
}
#property(nonatomic, retain) IBOutlet UIView *view2;
Inside the Interface Builder, I created a new View-based xib. Set file owner to SettingsRootController. Randomly drag a UITextView into the xib. Connect the UITextView to view and view2 in SettingsRootController.
However, if the above line is:
[self.view addSubview:_settingsController.view2];
a would always be 0, and thus the new screen is empty.
But if change to:
[self.view addSubview:_settingsController.view];
I could see the UITextView.
When you create a view controller, its view will not be created at initialisation time. The view will be created at first access of the view property. Thats why it works when you use _settingsController.view. But when you access view2 the view will not be loaded. You could write a custom getter method like:
-(UIView*) view2 {
_view2 = self.view;
return _view2;
}
The instance var declaration is different from the property declaration! With and without underscore. Maybe thats why it doesn't work.