IOS/Objective-C: Display uilabelview on two lines - ios

I am having trouble getting a uilabelview to always display on two lines. When the page first loads, the text only uses one line and is truncated. Once you load another view controller on top of it--for example an edit VC and then cancel the second VC, the label does flow to two lines which is what I want. However, I cannot get it to do first time.
Can anyone suggest what is issue? What could be different between the VC at first and then after you have shown and canceled another VC on top of it.
The label is created in storyboard. It is reference in code in viewwillappear as follows:
_myLabel.numberOfLines = 0;
NSString *nameToDisplay = _myItem.name;
[_myLabel sizeToFit];
[_myLabel setNeedsDisplay];

Related

How to reset ViewController on the press of a button in iOS

My app has a ViewController consisting multiple textfield and custom UI elements arranged as a form with "submit" and "reset" buttons.
I want to reset all the textfields and custom UI elements when user clicks the submit button so that user gets a feeling that same Form is opened again.
I tried calling ViewDidLoad() and setNeedsDisplay() on the click of "Submit" button but data previously filled by user remains as it is.
Kindly Help!
Just set the text of all textfields to an empty string (the text value). There is no magic function to clean everything in a view.
The only other solution would be to actually display the ViewController again, but this is probably not what you want, because it causes overhead as well as you might see something of the switching on the screen.
If you programmatically coded your textfields and some other objects, you can refresh always your viewController everytime by adding all your UI codes in the predefined method: -(void)viewWillAppear:(BOOL)animated{} and you should remove all self.view elements by adding this code.
[self.view.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
You codes in the viewWillAppear will look like this.
-(void)viewWillAppear:(BOOL)animated{
[self.view.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
// Codes for User Interface here
}

UITextView Will Not Scroll to Top

Update - I now believe this is due to me calling [view.details scrollRangeToVisible:NSMakeRange(0, 1)]; too early--meaning before the constrains have been applied and layouts recalculated.
I figured this much out by calling the scrollRangeToVisible after the view was shown and after I took some action on the cell. (This would guarantee that all layout changes were complete before I made that call.)
Thus, what I need to know now is Where should I make this call? such that it occurs after the layouts?
Original Post
I've tried many solutions, but none of them seem to work.
I have a UITextView as a subview to a UIView -- a container for the text view plus a few other items. When a user clicks on a cell in a tableView, I pop up the UIView container to show more details--the full text.
Configuring the view, I set the textView.text value to my long string, I update the font, I set the color, and I show the container view. Unfortunately, it shows the text at the bottom of the textview rather than at the top.
I've tried the following solutions, to no avail--each one results in the same behavior as above: it still shows the text at the bottom of the textview.
Attempt 1: scrollRangeToVisible
[self.textView scrollRangeToVisible:NSMakeRange(0,1)];
Attempt 2: contentOffset (tried each of the following, but none work)
[self.textView setContentOffset:
CGPointMake(0, -self.textView.contentInset.top) animated:YES];
[self.textView setContentOffset:CGPointMake(self.textView.contentOffset.x, 0)
animated:YES];
[self.textView setContentOffset:CGPointMake(0, self.textView.contentSize.height-textView.frame.size.height) animated:YES];
Am I fishing with those last items? Yes. I've run out of ideas. I've checked the values of contentOffset.y and contentInset.y -- both are zero.
I create the UITextView in a subclass of UIView called PrayerPopupView. Here's the process:
In my main UIViewController where the table exists, a user touches a cell. Based on this action...
I create an instance of PrayerPopupView in this view controller
In the creation of this view, the UITextView is created
The textView.text value is set.
I call `[self.details scrollRangeToVisible:NSMakeRange(0, 1)];
I use a custom class (KLCPopup) to add the view to the UIWindow but I also tested by adding my PrayerPopupView directly as a subview of the view controller rather than using KLCPopup, just in case it mattered. Both yield the same result of text not scrolled to the top correctly.
Since adding the scrollRangeToVisible didn't work in the custom view itself, I also added it in the UIViewController before showing the PrayerPopupView ([prayerPopupView.detailsTextView scrollRangeToVisible:NSMakeRange(0, 1)];), and still the same result.
Here's a screenshot of the view:
Here's a screenshot of the view properties in the debugger:
This shows the view hierarchy in the debugger:

Objective C: show textfield and textview input in label on next view?

So i've seen many questions alike mine, but all don't have the working answer, or have vague answers (and none of them marked as correct).
I have two ViewControllers:
ViewController #1 has 4 textFields to which there are pickerViews attached in order to choose an option to fill in the textfield.
Also there is 1 textView in which the user can use the keyboard.
To sketch the scenario: Label states: 'Pick a color:' User clicks on the textfield (pickerView shows up full of names of colors) user selects 'red' and the picked choice shows up in the textfield below the label: 'pick a color'.
Then when all textfields and textview are filled in, the user clicks save. In which the save button redirects the user to the 2nd ViewController.
ViewController #2:
This is where i want the input (of the textFields and textView) to be shown in the labels(if this is the correct usage). Since this is where the user will see list of the chosen answers. However i cannot manage to get this working.
What do i need to do in order to achieve this?
Also i'm still learning. Please bear with me.
Thanks in advance.
In general this site is not well-suited to "Tell me how to do XYZ" type questions. It's intended for getting help with code you have already written. You're new, though, so I'll take pity on you.
You should post the code that you have, showing how you get from view controller 1 to view controller 2.
I'm going to assume that you have a segue from view controller 1 to view controller 2.
(You should probably have code that disables your send button until all 4 text fields are filled in with valid colors.)
Anyway, let's assume that your send button triggers a segue, either directly, or in an IBAction that triggers a segue through a call to performSegueWithIdentifier.
You need to define properties for your 4 color strings in view controller 2.
Then, add a prepareForSegue method to view controller 1 if you don't have one already. In prepareForSegue, make sure the segue matches the segue identifier that invokes view controller 2 (good idea even if you only have 1 segue now, since you might add more segues later.)
ViewController2 *vc2 = (ViewController2 *) segue.destinationViewController;
Assuming it's the right segue, get a pointer to the destination view controller and cast it to the type of ViewController2 so you can refer to your color properties.
Then use code like this to pass the colors to view controller 2:
vc2.color1 = myColor1TextField.text;
vc2.color2 = myColor2TextField.text;
vc2.color3 = myColor3TextField.text;
vc2.color4 = myColor4TextField.text;

Delayed displaying of image view objective c

I am trying to keep my app to one view controller so i dont have to pass information back and forth. First the user uses text fields to fill in information and then my program processes it and displays labels on an image. Is it possible to have an image view hidden until a button is clicked and then bring the image forward and display the image view on top of the text fields?
imageView.alpha = 0; // Not visible
imageView.alpha = 1; // Visible
You can put it under obe of your UIButton action.
In order for it to be on top of everything just drag the image view to be at the lowest level inside the interface builder.

Add a bunch of controls dynamically to screen in iOS

I am working on a screen where user selects an option from drop-down. Based on this selection he adds a bunch of controls to screen. My drop-down is at the bottom-right corner of the screen. It takes up 20-25% of screen space at the bottom. In the remaining 75% screen at the bottom i need to add 9 labels,6 UItext fields ,2 drop-downs and 2 buttons. I am attaching a markup here.
My question is: in ASP.NET, I have a repeater which has all the above controls. In one of the buttons named "Add More". If I click that button, it adds the same repeater at the bottom of the current one. Do we have anything like that in iOS? Like a repeater which has a bunch of controls and click add and that exact repeater is added at the bottom and so-on and so forth. If any ideas/new ideas based on my explanation pop-up then please let me know. Thanks.
The link
http://dl.dropbox.com/u/40597849/mockup2.png
So, the big rectangle to the right containing 8 labels, textboxes, drop-downs and the 2 buttons below it(one is named as "Add More") should be added again when user clicks on Add More. Please ask me if you need more information.
ITMCustomView *cView = [[ITMCustomView alloc] initWithFrame:frame];
cView.delegate =self;
CGPoint scrollPoint = CGPointMake(0.0, (frame.origin.y/400)*400);
[self.scrollView setContentOffset:scrollPoint animated:YES];
[self.scrollView addSubview:cView];
Initially the very first custom view i add, i add it with this frame size (187, 585, 350, 21); . Next whenever i hit add more i call this delegate
[self.delegate addNewSize:CGRectMake(187,self.y+ 400 , 400, 400)];
This calls the above 5 lines of code and makes a new custom view and adds it to scroll view. I store the self.y as the y value of whatever frame currently being built. So how do i make my scroll view move up and down freely. The screen shows the custom view as they are added but doesnt allow me to go up anymore. I wanted to suppose add the custom view 5 times and i added 4 i just want to scroll up , see i added 4 scroll down and just add 1 more custom view and do other processing with it. But my scroll view is non-functional and its stuck at the just added custom view disabling me to scroll up. If you need more information, please ask me. Thanks.
Use a custom view that contains all your views.
1.) Make a custom class myView which subclasses UIView.
2.) Add all the controls you need in that view class.
3.) include myView in your main view.
4.) initiate as many myView as you like and add them to your main view.
I hope that is clear enough to get you going.
Update: As to elaborate more on the comment bgoers made.
When you create a new class myView sub-classing UIView you get an empty sheet. This sheet will have the frame that you specify either in the class or when you initiate (up to you). Within that frame (myView.h and myView.m in initWithFrame), you can add subViews like buttons or labels. Those subviews will placed relative to the sheet myView.
For example, your draw up is a custom view with all the controls. I can have another one looking exactly like it by simply initiating another custom view without adding more subviews. All I need to do is move the second custom view to where I would like, all the subviews will follow.
I encourage you to read up on how to subclass and how that works in iOS. It is very simple once you get the hang of it.

Resources