get a subview back after placing it into the view - objective-c

so in my app, i click a button and i dynamically add a UITextField:
myRect = CGRectMake(4, viewYPos, 80, 25);
UITextField *myTextField = [[UITextField alloc] initWithFrame:myRect];
myTextField.borderStyle = UITextBorderStyleRoundedRect;
myTextField.font = [UIFont fontWithName:#"Trebuchet MS" size:12];
myTextField.returnKeyType = UIReturnKeyDone;
myTextField.delegate = self;
[myTextField addTarget:self
action:#selector(textFieldShouldReturn:)
forControlEvents:UIControlEventEditingDidEndOnExit];
[attributeScrollView addSubview:myTextField];
[myTextField release];
now, how do i reference this text field? is there a way to pull it out of the view and place it back into a new textfield variable?
my only thought is to create an instance nsarray variable so i can keep their reference.

Make myTextField an instance variable instead of local. Then, don't release it.

Related

UITextField becomeFirstResponder triggers _resignFirstResponder

I am programatically creating a UITextField, adding it to view and then start editing by calling
[textField becomeFirstResponder];
But the issue is this call triggers call to textFieldDidEndEditing: delegate method. The stack trace points to [textField _resignFirstResponder] which is invoked by becomeFirstResponder. This is happening on simulator and iOS 7. How do I avoid this ? This is causing lot of issues as I don't want textFieldDidEndEditing: to be called without keyboard getting dismissed.
EDIT : Here is how I create UITextField.
UITextField *titleField = [[[UITextField alloc] init] autorelease];
[titleField addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
titleField.textColor = [UIColor colorWithWhite:1.f alpha:0.8];
titleField.font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:40.f];
titleField.textAlignment = NSTextAlignmentCenter;
titleField.borderStyle = UITextBorderStyleBezel;
titleField.autocorrectionType = UITextAutocorrectionTypeNo;
// titleField.borderStyle = UITextBorderStyleLine;
titleField.frame = CGRectMake(0, 0, 200, 80);
titleField.center = self.previewView.center;
titleField.delegate = self;
NSDictionary *titleDictionary = [self titleDictionary];
if (titleDictionary) {
titleField.text = [titleDictionary objectForKey:kTitleStringKey];
NSString *fontName = [titleDictionary objectForKey:kTitleFontNameKey];
CGFloat fontSize = [[titleDictionary objectForKey:kTitleFontSizeKey] floatValue];
titleField.font = [UIFont fontWithName:fontName size:fontSize];
NSData *textColorData = (NSData *)[titleDictionary objectForKey:kTitleTextColorKey];
titleField.textColor = [NSKeyedUnarchiver unarchiveObjectWithData:textColorData];
CGSize size = [titleField.text sizeWithFont:titleField.font];
titleField.frame = CGRectMake(0, 0, size.width + 40, 80);
titleField.center = self.previewView.center;
}
[self.view addSubview:titleField];
[titleField becomeFirstResponder];
Ok I found it. I had a UILongPressGestureRecognizer that triggered adding UITextField routine, and that is incidentally called twice on long press (need to figure out why, but its a separate issue). So becomeFirstResponder was called twice.

create and hide UILabel

I created a UILabel programatically and triggering a button I would like to hide the same label. This is my code:
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 320, 100)];
nameLabel.text = #"TEXT";
nameLabel.backgroundColor = [UIColor greenColor];
nameLabel.numberOfLines = 5;
nameLabel.font = [UIFont boldSystemFontOfSize:12];
[self.view addSubview:nameLabel];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:#"Hide" style:UIBarButtonItemStyleBordered target:self action:#selector(back)];
- (IBAction)back{
self.navigationItem.rightBarButtonItem=nil;
[nameLabel setHidden: YES]; not working
nameLabel.hidden = YES; not working
}
Am I missing something?
This is also another way to do same
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 320, 100)];
nameLabel.text = #"TEXT";
nameLabel.tag = 1001;
nameLabel.backgroundColor = [UIColor greenColor];
nameLabel.numberOfLines = 5;
nameLabel.font = [UIFont boldSystemFontOfSize:12];
[self.view addSubview:nameLabel];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:#"Hide" style:UIBarButtonItemStyleBordered target:self action:#selector(back)];
- (IBAction)back{
self.navigationItem.rightBarButtonItem=nil;
UILabel *tempLabel = (UILabel *)[self.view viewWithTag:1001];
[tempLabel setHidden: YES];
tempLabel.hidden = YES;
}
In order for the button to be accessible from other methods, you need to assign it to an instance variable (whether directly or via a property) rather than assigning it to a local variable. The proper way to declare the property is
#property(nonatomic, strong) UILabel *nameLabel;
which you can then assign to using
self.nameLabel = [[UILabel alloc] init...];
Later on, you can say
self.nameLabel.hidden = YES;
and it should work.
It's hard to know how that would even compile since the code you show to create nameLabel makes it local to whatever method that's in. Try making nameLabel a property and using self.nameLabel whenever you reference it, either creating it or touching its properties.

Adding list to a view without changing views

How can I add a list (TableView) to a view without changing views, I want to show the list over the current view but in the middle of the view, so the original view will be showed also.
I tried with label...but gettign some problems adding tableView instead of label.
UIView *testView = [[[UIView alloc] initWithFrame:CGRectMake(0,0, 100, 100)autorelease];
testView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.2f]; UILabel *label = [[[UILabel alloc] init] autorelease];
label.text = #"TEST";
[label sizeToFit];
[testView addSubview:label];
[self addSubview:testView ];
Refer mjpopupviewcontroller link as it is according to your requirement

UITextField Not Allowing Input

I am programatically adding a UITextField into a UIImageView. When I set the text property, I can see the text.
The issue is, it's not letting me input text into it like an input field when I run the app.
This is my code for initializing the TextField:
UITextField *textField = [[UITextField alloc] init];
textField.frame = CGRectMake(38.0f, 0.0f, self.view.frame.size.width - 38.0f, 40.0f);
textField.text = #"Hello";
textField.background = [[UIImage imageNamed: #"field.png"] stretchableImageWithLeftCapWidth:12 topCapHeight:19];
textField.backgroundColor = [UIColor whiteColor];
textField.userInteractionEnabled = YES;
[bar addSubview: textField];
Do I need to do something special to allow for text input?
If bar is an UIImageView, try to set
bar.userInteractionEnabled = TRUE;
Text Field not added into imageview . You can add textfield on view,for user interaction enabled.
First you can add imageview and then add text field on the view.
Like This--
[self.view addSubview:imgV];
[self.view addSubview: textField];

Where's the memory leak?

I'm adding a custom view to a tableHeaderView with the following code:
imageButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
imageButton.frame = CGRectMake(120, 12, 64, 64);
imageButton.titleLabel.font = [UIFont systemFontOfSize:10];
imageButton.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
imageButton.titleLabel.textAlignment = UITextAlignmentCenter;
[imageButton setTitle:NSLocalizedString(#"Choose\nPhoto", #"Choose\nPhoto") forState:UIControlStateNormal];
[imageButton addTarget:self action:#selector(photoButtonPressed) forControlEvents:UIControlEventTouchUpInside];
// add existing image, if any, to button
if (child.thumbnailImage != nil) {
[imageButton setBackgroundImage:child.thumbnailImage forState:UIControlStateNormal];
}
// add button to view
self.headerView = [[UIView alloc] initWithFrame:CGRectMake(22, 12, 70, 70)];
[headerView addSubview:imageButton];
// add view to table header
self.tableView.tableHeaderView = headerView;
The memory leak is showing on the alloc line above for the headerView UIView.
I'm declaring the UIView and the UIButton in the header file and releasing it in ViewDidUnload and dealloc, so I'm not sure what I'm doing wrong.
Also, this is only showing up on the device and not the simulator (just thought I'd mention).
Any help would be appreciated.
Thanks,
Rod
You're using the headerView setter method (self.headerView) so you need to release the UIView instance you assign to the headerView property, either using release or autorelease.
e.g.
UIView* newHeaderView = [[UIView alloc] initWithFrame...];
self.headerView = newHeaderView;
[newHeaderView release];
or
self.headerView = [[[UIView alloc] initWithFrame:...] autorelease];
The reason is because the headerView setter method automatically retains the object assigned to it.
Alternatively, you can set the headerView instance variable directly, without using the property setter method:
[headerView release]; // release any existing object
headerView = [[UIView alloc] initWithFrame:...];