how to expand the dynamically created UIviews on clicking of the UIButton - iphone

I have created dynamic UIView with scrollview and UIView is having subview UIbutton.
my requirement is on clicking of the UIButton I need to show one more UIView in between the dynamic UIviews.
I need to know how to change the dynamic UIView frame size dynamically on clicking of the UIbutton.
Thanks in advance.
here is the samples
NSArray *subViews = [createIssueScroll subviews];
NSLog(#"%#",subViews);
NSInteger tag=[sender tag];
NSLog(#"%d", tag);
UIView *vw = [createIssueScroll viewWithTag:tag];
[self createIssueSubView:[vw frame]];
UIView *sVW = [createIssueScroll viewWithTag:(tag+1)];
NSLog(#"%#", sVW);
float y;
y = sVW.frame.origin.y;
sVW.frame = CGRectMake(0, y, sVW.frame.size.width, sVW.frame.size.height);
NSLog(#"%f", y);
code explanation
i have created button through programatically, i will explain in detail above code
In first line i have taken all the scrollview subviews(dynamically created UiViews)
i have button tag value
and add a method of UIsubviews(this subview is displaying on clicking of ui button)
suppose i have click dynamically created first Uiview button, need to show subview after the first dynamic UIview and second dynamic UIview is push to down side.

Find subViews by using foreach
UIView *firstView;
UIView *seconfView;
for(id currentview in scrollView.subviews)
{
if([currentview isKindOfClass:[UIView class]])
{
UIView *view = (UIView *)currentview;
if(view.tag == 1) //1 is your view tag
{
firstView = view;
}
if(view.tag == 2) //2 is your view tag
{
seconfView = view;
}
}
}
Now you can use first and second views :)

Related

Can i reuse UIView?

I have UIView in my storyboard with outlet mynewView and i want it display with totally different content. And do it many times.
My UIView contains many objects, i load it from internet. But every time i do
NSArray *viewsToRemove = [view1 mynewView];
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
}
//Let's try to remove all possible views..
viewsToRemove = [self.view subviews];
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
}
//here i create mynewView
mynewView = [self createnewRandomView];
//And finally add the View we have created.
[self.view addSubview:mynewView];
i monitor the memory consumption and i see it grows every time i add new UIView. Even when i remove all superviews. I guess they remain in memory.
Before i change the view i remove from superview all possible views, but they still stay in memory.
Xcode is set to use ARC so i can't release it.
Is there any good way to reuse a UIView?
Thanks
[self.view addSubview:mynewView];
Your newView was added as subview of self.view.
Your above code does not use for remove yourVewView in self.view.
Please try this code before adding new view:
for (UIView *view in self.view) {
if([view isKindOfClass:[YourNewViewClassName class]])
{
[view removeFromSuperView];
break;
}
}
As you remove the subviews, set them to nil as you go, like so:
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
v = nil;
}
For more information, see for example Arc: Setting references to nil, for multiple buttons

UItextView hide in UIScrollview on tap

I have an UIScrollView (with 40+ pages) with UITextview added as subview on every page.
What i want to do is that on single tap, i want to hide the UITextField on all pages?
How should I do it?
I am trying to do like this in hadle tap method:
textview.hidden = YES;
But it is hiding text view only on last page, not on all pages.
Any help is appreciated
You can do, instead of textview.hidden = YES; the following:
for(id object in objScrollView.subviews)
{
if([object isKindOfClass:[UITextView class]])
{
UITextView *tmpObj= (UITextView *)object;
[tmpObj setHidden:!tmpObj.hidden];
}
}
Put the above code in tap method.

Adding a view dynamically with in the same view

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

Adding View into scroll view

I have created one scroll view and on the scroll view, i am adding two alternate custom UIView s
#interface FirstView : UIView
#interface SecondView : UIView
my array is
NSMutableArray *classArray= [[NSMutableArray alloc] initWithObjects:firstView, secondView, firstView, secondView, nil];
i am adding views on scroll view in following manner
for ( i = 0 ; i< 5; i++)
{
UIView *userView = [classArray objectAtIndex:i];
CGRect rect;
rect.size.height = KVIEWHGT;
float userViewWidth = userView.frame.size.width;
rect.size.width = userViewWidth;
userView.frame = rect;
[scrollView addSubview:userView];
[userView release];
}
UIView *userView = nil;
NSArray *userSubViews = [scrollView subviews];
CGFloat x = 0;
float userViewWidth = 0.0f;
for (userView in userSubViews) {
if ([userView isKindOfClass:[UIView class]]){
CGRect frame = userView.frame;
frame.origin = CGPointMake(x, 0);
userView.frame = frame;
userViewWidth = userView.frame.size.width;
x += userViewWidth + 10;
}
}
[scrollView setContentSize:CGSizeMake(x, scrollView.frame.size.height)];
I am getting output as only two custom views on scroll view but i want there should be 5 custom view on the scroll views
Please help me to solve this problem.
You are adding firstView ,secondView multiple time in the array, even though you add it multiple time it refers to the same instance
NSMutableArray *classArray= [[NSMutableArray alloc] initWithObjects:firstView, secondView, firstView, secondView, nil];
A UIView can only have one parent view. You are using the same UIView instance multiple times. If you add a UIView to another, it is removed from the one it is currently attached to.
Read UIView Class Reference, addSubview:
Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview.
even if it works, you would have duplicated views in your scrollview. you have to create x instances of you views and add them to your scrollview.
thats kind of creepy code anyway.
1.
UIView *userView = [classArray objectAtIndex:i];
[userView release];
you dont have to release this, because its not yours!
2: why iterating twice over the views? makes no sense, better
NSMutableArray* myArray = [[NSMutableArray alloc] initWithObjects: firstView,secView,thirdView,fourthView];
for (UIView* view in myArray)
{
//set the Frame and add it the the ScrollView
}

Change subview on UIView ? [close]

i have success show subview from another NIB, i have 2 button and 1 UIView on MainView, when i klick button 1 will show NIB1 on UIView, and then i klick button 2 will show NIB2 on UIView, but when i klick again button 1 UIView not show NIB1 again, but still NIB 2, this is my Code :
Updated Solved
On .h file
IBOutlet UIView *bottomView;
On .m file
-(IBAction) handleButton1{
for (UIView *t in bottomView.subviews) {
[t removeFromSuperview];
}
if (![nib1 isViewLoaded]) {
nib1 = [[NIB1 alloc] initWithNibName:#"NIB1" bundle:nil];
}
[bottomView addSubview:nib1.view];
}
-(IBAction) handleButton2{
for (UIView *t in bottomView.subviews) {
[t removeFromSuperview];
}
if (![nib2 isViewLoaded]) {
nib2 = [[NIB2 alloc] initWithNibName:#"NIB2" bundle:nil];
}
[bottomView addSubview:nib2.view];
}
How i can refresh UIView with another NIB on my code ?
Thanks,
I have solve this case, i remove all subview on MainView from this http://stackoverflow.com/questions/3915729/refreshing-cell-content-after-deleting-cells
use :
for (UIView *t in bottomView.subviews) {
[t removeFromSuperview];
}
I hope this code help anyone :).
You have to remove the first view view [theViewYouDontWantToShow removeFromSuperview].
Or you can use [bottomView bringSubviewToFront:nib1/2.view].
What was appending here is only that you added a view (this works), added the other view on top of the first one (this works too), but then you added the first one again. The view doesn't change since it's already there.