05-31-2010, 02:00 PM
#1 (permalink )
Registered Member
Join Date: Apr 2010
Posts: 55
Removing a UILabel
Hello,
I have the following code in my didSelectRowAtIndexPath:
Code:
UILabel *partyDetails = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
partyDetails.font = [UIFont fontWithName:@"Zapfino" size: 14.0];
partyDetails.textAlignment = UITextAlignmentCenter;
partyDetails.backgroundColor = [UIColor clearColor];
partyDetails.shadowColor = [UIColor grayColor];
partyDetails.shadowOffset = CGSizeMake(1,1);
partyDetails.lineBreakMode = UILineBreakModeWordWrap;
partyDetails.numberOfLines = 0;
partyDetails.text = [NSString stringWithFormat:@"%@", [partydetailArray objectAtIndex:row]];
[viewController.view addSubview:partyDetails];
[partyDetails release];
When a you select table row a specific string is called from an array and that is displayed in my UILabel. But as soon as you go back and select another row, the new label is added on top of the old one. I know I can remove the label with:
Code:
[partyDetails removeFromSuperview];
But adding that just erases the label without displaying it. Any ideas on how I can effectively erase the label?
05-31-2010, 02:55 PM
#2 (permalink )
A Single-Serving Friend
Join Date: Mar 2010
Location: Groningen, NL
Posts: 491
I am not sure whether I understand what you want but if I do, something as simple as this might help:
partyDetails.text = @"";
Sorry if I misunderstood.
Cheers,
Bob
__________________
We are God’s middle children, according to Tyler Durden, with no special place in history and no special attention.
Consider saying
thanks by buying
my app . :]
05-31-2010, 05:52 PM
#3 (permalink )
Registered Member
Join Date: Apr 2010
Posts: 55
Thanks for the reply Bob, but I'm looking for a statement that checks if the label exists, if not, create it and set all the properties of it except the text. Any one have an idea on how to accomplish this?
06-01-2010, 08:54 AM
#4 (permalink )
A Single-Serving Friend
Join Date: Mar 2010
Location: Groningen, NL
Posts: 491
I reckon you could check whether your label is nil. If it is, create it, if it isn't it already exists.
Code:
if (partyDetails == nil) {
// create it...
}
Hope this helps.
Cheers,
Bob
__________________
We are God’s middle children, according to Tyler Durden, with no special place in history and no special attention.
Consider saying
thanks by buying
my app . :]
06-01-2010, 05:40 PM
#5 (permalink )
Registered Member
Join Date: Apr 2010
Posts: 55
Quote:
Originally Posted by
Robert Paulson
I reckon you could check whether your label is nil. If it is, create it, if it isn't it already exists.
Code:
if (partyDetails == nil) {
// create it...
}
Hope this helps.
Cheers,
Bob
I'm still a little confused on how that can get me what I want. I have this in my .m file:
Code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger row = [indexPath row];
if (self.viewController == nil) {
AppViewController *details = [[AppViewController alloc] initWithNibName:@"PartyIdeasDetailView" bundle:nil];
self.viewController = details;
[details release];
}
viewController.title = [NSString stringWithFormat:@"%@", [partyideasArray objectAtIndex:row]];
UITextView *partyDetails = [[UITextView alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
partyDetails.font = [UIFont fontWithName:@"Zapfino" size: 14.0];
partyDetails.textAlignment = UITextAlignmentCenter;
partyDetails.backgroundColor = [UIColor clearColor];
partyDetails.editable = NO;
partyDetails.text = [NSString stringWithFormat:@"%@", [partydetailArray objectAtIndex:row]];
[viewController.view addSubview:partyDetails];
[partyDetails release];
And want partyDetails to "refresh" everytime a user goes back to the tableview and picks a new row. What I have so far just puts the partyDetails string on top of the last one picked. So when the user goes back to the tableview to choose a new row, the partyDetails uitextview needs to be erased.
06-01-2010, 06:07 PM
#6 (permalink )
A Single-Serving Friend
Join Date: Mar 2010
Location: Groningen, NL
Posts: 491
Sorry if I can't give spot-on advise - I am not so sure how to do that either. Just trying to share my thoughts on it as I thought that's better than nothing.
Quote:
What I have so far just puts the partyDetails string on top of the last one picked.
Every time the method is called, you create a new partyDetails text view. You add it to the view controller's view and then release it. However, the view controller's view now retains it!
When the method is called again, the previous partyDetails text view is still retained/alive and the new one is simply added on top of it (as you mentioned).
Could you make partyDetails an instance variable that you only update every time the method is called? That way you'd only have a single text view to care about instead of creating an entirely new one every time.
So, in your .h you'd declare partyDetails as an UITextView and then in your viewDidLoad:
Code:
partyDetails = [[UITextView alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
partyDetails.font = [UIFont fontWithName:@"Zapfino" size: 14.0];
partyDetails.textAlignment = UITextAlignmentCenter;
partyDetails.backgroundColor = [UIColor clearColor];
partyDetails.editable = NO;
Because that's what you do every time, right?
The only thing that really needs to change every time a row is selected is:
Code:
partyDetails.text = [NSString stringWithFormat:@"%@", [partydetailArray objectAtIndex:row]];
... so that'd stay in your
tableView:didSelectRowAtIndexPath: .
Hope this helps.
Cheers,
Bob
__________________
We are God’s middle children, according to Tyler Durden, with no special place in history and no special attention.
Consider saying
thanks by buying
my app . :]
06-01-2010, 09:36 PM
#7 (permalink )
Registered Member
Join Date: Apr 2010
Posts: 55
I think you were on track when you posted
Code:
if (partyDetails == nil) {
// create it...
}
I was posting this question around and I got a reply about the above code and what I wanted saying:
Quote:
Sort of, but that alone won't be sufficient. 'partyDetails' is defined within your method so you're going to lose your reference to that label at the end of the method. The second time didSelectRowAtIndexPath is called it won't have any reference to the label you created the first time you called didSelectRowAtIndexPath. You need to declare your variables in a scope appropraite to the way you want to use them. If you expect a variable to persist between method calls then it should be a instance variable of the class.
But I'm not quite sure what that means...
06-01-2010, 09:50 PM
#8 (permalink )
A Single-Serving Friend
Join Date: Mar 2010
Location: Groningen, NL
Posts: 491
Hi, vund0
it says pretty much the same I said: Make it an instance variable.
Code:
// in your .h
@interface YourViewController : UIViewController {
UITextView *partyDetails;
}
@property (nonatomic, retain) UITextView *partyDetails;
@end
and
Code:
// in your .m
@synthesize partyDetails;
-(void) viewDidLoad {
partyDetails = [[UITextView alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
partyDetails.font = [UIFont fontWithName:@"Zapfino" size: 14.0];
partyDetails.textAlignment = UITextAlignmentCenter;
partyDetails.backgroundColor = [UIColor clearColor];
partyDetails.editable = NO;
}
This way, partyDetails will be accessible from all methods in your .m file. If you alloc and init it in the method that corresponds to the selection of a row, the text view will only exist there. If you try to "contact" it from somewhere else, you can't reach it.
Hope this helps.
Cheers,
Bob
__________________
We are God’s middle children, according to Tyler Durden, with no special place in history and no special attention.
Consider saying
thanks by buying
my app . :]
06-01-2010, 10:01 PM
#9 (permalink )
Registered Member
Join Date: Apr 2010
Posts: 55
Thanks for the help Bob. So then I put this under didSelectRowAtIndexPath right?
Code:
partyDetails.text = [NSString stringWithFormat:@"%@", [partydetailArray objectAtIndex:row]];
[viewController.view addSubview:partyDetails];
[partyDetails release];
06-02-2010, 08:55 AM
#10 (permalink )
A Single-Serving Friend
Join Date: Mar 2010
Location: Groningen, NL
Posts: 491
If you have partyDetails as an instance variable, you don't need to add it to the view anymore. And since you are not creating it again at that point, you don't need to release it either. The only thing you seem to need in your didSelectRowAtIndexPath: is this,
Code:
partyDetails.text = [NSString stringWithFormat:@"%@", [partydetailArray objectAtIndex:row]];
because you only want to update the text of the text view, right?
Hope this helps.
Cheers,
Bob
__________________
We are God’s middle children, according to Tyler Durden, with no special place in history and no special attention.
Consider saying
thanks by buying
my app . :]
06-02-2010, 02:26 PM
#11 (permalink )
Registered Member
Join Date: Apr 2010
Posts: 55
Right, but when I only put
Code:
partyDetails.text = [NSString stringWithFormat:@"%@", [partydetailArray objectAtIndex:row]];
in didSelectRowAtIndexPath, nothing shows up. It's when I add
Code:
[viewController.view addSubview:partyDetails];
that anything shows up.
06-02-2010, 02:27 PM
#12 (permalink )
A Single-Serving Friend
Join Date: Mar 2010
Location: Groningen, NL
Posts: 491
That sounds to me as if you didn't properly set it up as an instance variable. Please post your .h and .m file.
Cheers,
Bob
__________________
We are God’s middle children, according to Tyler Durden, with no special place in history and no special attention.
Consider saying
thanks by buying
my app . :]
06-02-2010, 02:45 PM
#13 (permalink )
Registered Member
Join Date: Apr 2010
Posts: 55
Code:
//My .h file
#import <UIKit/UIKit.h>
@interface Partyideas : UITableViewController <UITableViewDelegate, UITableViewDataSource>
{
UITextView *partyDetails;
}
@property (nonatomic, retain) UITextView *partyDetails;
@end
Code:
//My .m file
@implementation Partyideas
@synthesize partyDetails;
- (void)viewDidLoad {
[super viewDidLoad];
partyDetails = [[UITextView alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
partyDetails.font = [UIFont fontWithName:@"Zapfino" size: 14.0];
partyDetails.textAlignment = UITextAlignmentCenter;
partyDetails.backgroundColor = [UIColor clearColor];
partyDetails.editable = NO;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger row = [indexPath row];
if (self.viewController == nil) {
MainViewController *details = [[MainViewController alloc] initWithNibName:@"PartyIdeasDetailView" bundle:nil];
self.viewController = details;
[details release];
}
//imageView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 0, 200, 50)];
//image = [[UIImage alloc] initWithImage:[imagesArray objectAtIndex:row]];
viewController.title = [NSString stringWithFormat:@"%@", [partyideasArray objectAtIndex:row]];
partyDetails.text = [NSString stringWithFormat:@"%@", [partydetailArray objectAtIndex:row]];
MainAppAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.mainDayNavController3 pushViewController:viewController animated:YES];
06-02-2010, 03:03 PM
#14 (permalink )
Registered Member
Join Date: Apr 2010
Posts: 55
So just from playing around with it, I found I get the result I want when I add
Code:
[viewController.view addSubview:partyDetails];
to didSelectRowAtIndexPath: So everything works now... As far as memory management goes, do I need to release partyDetails in dealloc?
06-02-2010, 03:39 PM
#15 (permalink )
A Single-Serving Friend
Join Date: Mar 2010
Location: Groningen, NL
Posts: 491
Yes, you need to release it in dealloc.
Well, glad it works now.
Cheers,
Bob
__________________
We are God’s middle children, according to Tyler Durden, with no special place in history and no special attention.
Consider saying
thanks by buying
my app . :]
06-02-2010, 03:46 PM
#16 (permalink )
Registered Member
Join Date: Apr 2010
Posts: 55
Thanks for your help Bob
06-02-2010, 03:52 PM
#17 (permalink )
A Single-Serving Friend
Join Date: Mar 2010
Location: Groningen, NL
Posts: 491
You're welcome, mate.
__________________
We are God’s middle children, according to Tyler Durden, with no special place in history and no special attention.
Consider saying
thanks by buying
my app . :]
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 329
9 members and 320 guests
ajay123123 , ashaman64 , baja_yu , ChrisYates , HemiMG , newDev , Objective Zero , pkIDSF , Steven.C
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,648
Threads: 94,113
Posts: 402,878
Top Poster: BrianSlick (7,990)
Welcome to our newest member, brandon6031