I have a UITextField and UITextView in the same view but I'm having trouble with them being editable, especially when going from one to the other.
If I tap in the UITextField, I'm able to type in it. But if I then type into the UITextView, then it doesn't capture what I'm typing. And the same happens visa-versa.
Any ideas why it is behaving like this?
review = [[UITextView alloc] initWithFrame:CGRectMake(x+10, y + 40, 470-5, h - 15)];
review.keyboardType = UIKeyboardTypeAlphabet;
review.delegate = self;
review.font = [UIFont systemFontOfSize:16];
[scrollView addSubview:review];
and
titleField = [[UITextField alloc] initWithFrame:CGRectMake(x+10, y + 30, 472, 32)];
titleField.keyboardType = UIKeyboardTypeAlphabet;
titleField.clearButtonMode = UITextFieldViewModeWhileEditing;
titleField.delegate = self;
titleField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
titleField.font = [UIFont systemFontOfSize:14];
[scrollView addSubview:titleField];
Both of these are global variables and their sizes are large enough.
So I figured it out, but I'm not sure why it is this way.
The objects were being released such that my pointers weren't accurate anymore?
My .h stayed the same:
{
UITextField *titleField;
UITextView *review;
}
#property (nonatomic, retain) UITextField *titleField;
#property (nonatomic, retain) UITextView *review;
And I changed both to be like this:
UITextField *titleField1 = [[UITextField alloc] initWithFrame:CGRectMake(x+10, y + 30, 472, 32)];
titleField1.keyboardType = UIKeyboardTypeAlphabet;
titleField1.clearButtonMode = UITextFieldViewModeWhileEditing;
titleField1.delegate = self;
titleField1.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
titleField1.font = [UIFont systemFontOfSize:14];
[scrollView addSubview:titleField1];
titleField = [titleField1 retain];
But doesn't this leave the object hanging even after I release titleField?
You should not use both
{
UITextField *titleField;
UITextView *review;
}
#property (nonatomic, retain) UITextField *titleField;
#property (nonatomic, retain) UITextView *review;
USE:
#property (nonatomic, retain) IBOutlet UITextField *titleField;
#property (nonatomic, retain) IBOutlet UITextView *review;
Related
When I input the following code, the textfields I added are not being displayed in the scrollview:
.h
#interface FirstViewController : UIViewController <UIScrollViewDelegate>
#property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
#property (strong, nonatomic) UITextField *firstField;
#property (strong, nonatomic) UITextField *secondField;
#property (strong, nonatomic) UITextField *thirdField;
.m (viewDidLoad)
self.firstField.placeholder = #"First";
self.firstField.textAlignment = NSTextAlignmentCenter;
self.firstField.font = [UIFont fontWithName:#"Helvetica Neue Light" size:50.0];
self.secondField.placeholder = #"Second";
self.secondField.textAlignment = NSTextAlignmentCenter;
self.secondField.font = [UIFont fontWithName:#"Helvetica Neue Light" size:50.0];
self.thirdField.placeholder = #"Third";
self.thirdField.textAlignment = NSTextAlignmentCenter;
self.thirdField.font = [UIFont fontWithName:#"Helvetica Neue Light" size:50.0];
[self.scrollView addSubview:self.firstField];
[self.firstField autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero excludingEdge:ALEdgeRight];
[self.firstField autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self.scrollView];
[self.firstField autoMatchDimension:ALDimensionHeight toDimension:ALDimensionHeight ofView:self.scrollView];
[self.scrollView addSubview:self.secondField];
[self.secondField autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.firstField];
[self.secondField autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self.scrollView];
[self.secondField autoMatchDimension:ALDimensionHeight toDimension:ALDimensionHeight ofView:self.scrollView];
[self.scrollView addSubview:self.thirdField];
[self.thirdField autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.secondField];
[self.thirdField autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero excludingEdge:ALEdgeLeft];
[self.thirdField autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self.scrollView];
[self.thirdField autoMatchDimension:ALDimensionHeight toDimension:ALDimensionHeight ofView:self.scrollView];
self.automaticallyAdjustsScrollViewInsets = NO;
I have self.scrollView is within a scrollView by the way (in a pageviewcontroller). I am using PureLayout to position the text fields, and I'm pretty sure the way I did it is correct. Is there something I'm missing?
yes you are missing some code,if you wan't to add UITextField programmatically you must to set size and position on you're view, something like that:
self.firstField.frame = CGRectMake(10, 200, 300, 40);
I hope this will help you.
AutoLayout work slightly differently with UIScrollView: It relate to its contentView instead of its frame. This is explained here.
You will need to set your UIScrollView's contentView property since its CGSizeZero by default. Then if you want your 3 UITextfields to appear side by side, you'll have to change their width constraint because otherwise they will fill the whole contentSize.
So for my iOS app, if I delete the textfields and the code which grabs the text field values, the button works--in that it presents the indicated view controller..
But when I add these textfields and try to get the button to also Log their values, the button no longer works in that I push the button and nothing happens.
It's also worth mentioning that the code which creates these textfields is littered with "local declaration of "X" hides instance variable" warnings.. but i hear that's not too important (?)
Here's what's in the .h file:
#interface RootViewController : UIViewController
#property (nonatomic, retain) IBOutlet UITextField *usernameTextfield;
#property (nonatomic, retain) IBOutlet UITextField *passwordTextfield;
- (IBAction)buttonClicked: (id)sender;
#end
And in the .m file:
#import "RootViewController.h"
#import "chooseFeedItemViewController.h"
#interface RootViewController ()
#end
#implementation RootViewController
#synthesize passwordTextfield;
#synthesize usernameTextfield;
- (void)viewDidLoad {
[super viewDidLoad];
UIView* v = self.view;
v.backgroundColor = [UIColor whiteColor];
//load Feed View button
CGRect buttonFrame = CGRectMake(177, 250, 100, 100);
UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
[button setTitle: #"submit" forState: UIControlStateNormal];
[button setTitleColor: [UIColor blueColor] forState: UIControlStateNormal];
[button addTarget: self
action: #selector(buttonClicked:)
forControlEvents: UIControlEventTouchDown];
[v addSubview: button];
//username textfield
IBOutlet UITextField *usernameTextfield = [[UITextField alloc] initWithFrame:CGRectMake(45, 30, 200, 40)];
usernameTextfield.textColor = [UIColor colorWithRed:0/256.0 green:84/256.0 blue:129/256.0 alpha:1.0];
usernameTextfield.font = [UIFont fontWithName:#"Helvetica-Bold" size:25];
usernameTextfield.backgroundColor=[UIColor whiteColor];
usernameTextfield.placeholder=#"create username";
//password textfield
IBOutlet UITextField *passwordTextfield = [[UITextField alloc] initWithFrame:CGRectMake(45, usernameTextfield.frame.origin.y+75, 200, 40)];
passwordTextfield.textColor = [UIColor colorWithRed:0/256.0 green:84/256.0 blue:129/256.0 alpha:1.0];
passwordTextfield.font = [UIFont fontWithName:#"Helvetica-Bold" size:25];
passwordTextfield.backgroundColor=[UIColor clearColor];
passwordTextfield.placeholder=#"create password";
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(15, 80, 400, 400)];
[view addSubview:usernameTextfield];
[view addSubview:passwordTextfield];
[v addSubview:view];
}
-(IBAction) buttonClicked:(id)sender {
NSString *password = [passwordTextfield text];
NSString *username = [usernameTextfield text];
NSLog(#"username = %# and password = %#", username, password);
[self presentViewController:[ChooseFeedItemViewController new]
animated:YES completion:nil];
}
There's more to the .m file but it's irrelevant, i think
alloc init give you brand spanking new objects, NOT references to your existing ones. Use self.usernameTextField to access your existing object.
You are in completely over your head.
Do not create new text fields. Set up your text fields in Interface Builder (IB), and control-drag from your IB fields into the header of your program to create IBOutlets. Then use those IBOutlets in your code.
It does matter, and matter a great deal, that you are getting "local declaration of "X" hides instance variable" warnings.
The problem is that you have instance variables for your text fields, and then you have code that creates an unrelated set of text fields and installs them in your form. If you also created text fields in IB, then you've got duplicates of each text field, but only one is connected to your IBOutlet instance variables. If you never created the text fields in IB, then your outlets point to nil.
You should stop, find a good book or set of video tutorials on Objective C and/or iOS development, and go through the exercises to learn the basics. Your lack of understanding is more than a few forum questions is going to fix.
I created a view to show in MapView annotation callout.
If I didn't use xib and instantiate this view with initWithFrame, then there is no problem.
But if I use xib, size shown on map is bigger, than is should be.
Code in view:
#interface CalloutContentView : UIView
#property (nonatomic) Visit *visit;
+ (id)customView;
#end
#import "CalloutContentView.h"
#interface CalloutContentView ()
#property (weak, nonatomic) IBOutlet UILabel *title;
#property (weak, nonatomic) IBOutlet UILabel *subtitle;
#property (weak, nonatomic) IBOutlet UISegmentedControl *sc;
#end
#implementation CalloutContentView
+ (id)customView
{
CalloutContentView *customView = [[[NSBundle mainBundle] loadNibNamed:#"CalloutContentView" owner:self options:nil] lastObject];
if ([customView isKindOfClass:[CalloutContentView class]])
return customView;
else
return nil;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// _title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, w, 18)];
_title.font = [UIFont boldSystemFontOfSize:15.0];
// _title.textAlignment = NSTextAlignmentLeft;
_title.textColor = [UIColor colorWithWhite:0.199 alpha:1.000];
_title.numberOfLines = 1;
_title.backgroundColor = [UIColor yellowColor];
[self addSubview:_title];
// _subtitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 25.0, w, 14)];
_subtitle.font = [UIFont systemFontOfSize:12.0];
_subtitle.textAlignment = NSTextAlignmentLeft;
_subtitle.textColor = [UIColor colorWithWhite:0.239 alpha:1.000];
_subtitle.backgroundColor = [UIColor clearColor];
[self addSubview:_subtitle];
// _sc = [[UISegmentedControl alloc]initWithItems:#[#"One",#"Two"]];
_sc.frame = CGRectMake(0, self.frame.size.height - 30, w, 30);
[_sc addTarget:self action:#selector(segmentedControlValueDidChange:) forControlEvents:UIControlEventValueChanged];
[self addSubview:_sc];
}
return self;
}
# end
And in MapViewController:
- (void)popupMapCalloutView {
// Change this for creating your Callout View
CalloutContentView *customView = [CalloutContentView customView];
// CalloutContentView *customView = [[CalloutContentView alloc]initWithFrame: CGRectMake(0, 0, 10, 90)];
// if you provide a custom view for the callout content, the title and subtitle will not be displayed
_calloutView.contentView = customView;
VisitAnnotationView *bottomPin = _visitAnnotationViews[_idAnn];
bottomPin.calloutView = _calloutView;
[_calloutView presentCalloutFromRect:bottomPin.bounds
inView:bottomPin
constrainedToView:_mapView
permittedArrowDirections:SMCalloutArrowDirectionAny
animated:YES];
}
That's how it look like when xib is used:
I have a custom UITableViewCell that contains several labels and an image view that can be one of three icons depending on a value in each item represented by the cell. All of my labels are updating properly, but the value of the image name is always nil. The image name is declared as a property of type NSString.
The custom cell is called FavoriteCell and all the values are setting properly except the NSString value "imgName"
FavoriteCell.h:
#interface FavoriteCell : UITableViewCell
#property (nonatomic, strong) UILabel *lblMainTitle;
#property (nonatomic, strong) UILabel *lblGaugeID;
#property (nonatomic, strong) UILabel *lblGaugeLastUpdate;
#property (nonatomic, strong) UILabel *lblGaugeHeight;
#property (nonatomic, strong) UILabel *lblGaugeCFS;
#property (nonatomic, strong) UIImage *bgImage;
#property (nonatomic, strong) UIImageView *goodToGoImage;
#property (nonatomic, strong) NSString *imgName;
#end
FavoriteCell.m:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
bgImage = [UIImage imageNamed:#"tableCellBG.png"];
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
UIColor *transparentBG = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
CGSize size = self.contentView.frame.size;
//self.backgroundView = [[UIImageView alloc] initWithImage:bgImage];
self.lblMainTitle = [[UILabel alloc] initWithFrame:CGRectMake(8.0, -10, size.width-20, size.height-40)];
[self.lblMainTitle setFont:[UIFont boldSystemFontOfSize:15]];
[self.lblMainTitle setTextAlignment:NSTextAlignmentLeft];
[self.lblMainTitle setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
[self.lblMainTitle setBackgroundColor:transparentBG];
self.lblGaugeID = [[UILabel alloc] init];
self.lblGaugeLastUpdate = [[UILabel alloc] initWithFrame:CGRectMake(9, 14, size.width-40, size.height)];
[self.lblGaugeLastUpdate setFont:[UIFont systemFontOfSize:14]];
[self.lblGaugeLastUpdate setBackgroundColor:transparentBG];
self.lblGaugeHeight = [[UILabel alloc] initWithFrame:CGRectMake(8, 45, size.width-40, size.height)];
[self.lblGaugeHeight setFont:[UIFont boldSystemFontOfSize:21]];
[self.lblGaugeHeight setBackgroundColor:transparentBG];
self.lblGaugeCFS = [[UILabel alloc] initWithFrame:CGRectMake(120, 45, size.width-40, size.height)];
[self.lblGaugeCFS setFont:[UIFont boldSystemFontOfSize:21]];
[self.lblGaugeCFS setBackgroundColor:transparentBG];
NSLog(#"IMAGE NAME: %#\n", imgName); // imgName is nil
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(220, 40, 40, 40)];
[imgView setImage:[UIImage imageNamed:imgName]];
[self.contentView addSubview:lblMainTitle];
[self.contentView addSubview:lblGaugeHeight];
[self.contentView addSubview:lblGaugeLastUpdate];
[self.contentView addSubview:lblGaugeCFS];
[self.contentView addSubview:imgView];
self.editingAccessoryType = UITableViewCellAccessoryDetailButton;
}
return self;
}
The values are being set in my table view controller's cellForRowAtIndexPath method shown here. For now, I'm just hard coding a value for testing. The hard coded value is not carrying over to the custom cell.
-(FavoriteCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
FavoriteCell *cell = (FavoriteCell*)[tableView dequeueReusableCellWithIdentifier:cellID];
if(cell == nil){
cell = [[FavoriteCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
Favorite *fave = [allFavoriteObjects objectAtIndex:indexPath.row];
NSString *currFeet = [NSString stringWithFormat:#"%.02fft", [fave.currentFeet doubleValue]];
NSString *currFlow = [NSString stringWithFormat:#"%dcfs", [fave.currentCFS intValue]];
[cell setImgName:#"thumbsUp.png"];
[cell.lblMainTitle setText:[fave stationRealName]];
[cell.lblGaugeID setText:[fave stationIdentifier]];
[cell.lblGaugeCFS setText:currFlow];
[cell.lblGaugeHeight setText:currFeet];
return cell;
}
Have I missed something obvious? Thanks!
that is normal because you code is running when you don't have set the imgName, you set it then that your init code is run.
fox fix it remove the code UIImage and the NSString from you custom cell and set it into your imageView then when you make the cell into the -tableView:cellForRowAtIndexPath: by imageView.image = [UIImage imageNamed:#"thumbsUp.png"];
Well, upon first glance, the moment at which you are saying imgName logs as nil seems to be because you are not actually setting the imgName until after initWithStyle is called. Or is there something I'm missing about what you're expecting to happen?
I recently have been asking questions pertaining to UIToolbars and what not, but now I discover I need to add items to it programatically, I have seen other people's methods on how to go about doing it, but when I try doing the same thing, nothing ends up appearing. Pinpointing this problem is what I need help with. Here are my connections in IB:
And here is the relevant code:
Header file:
#import <UIKit/UIKit.h>
#interface ParkingRootViewController : UIViewController {
UINavigationController *navigationController;
UIToolbar *toolbar;
UIBarButtonItem *lastUpdateLabel;
}
#property(nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
#property (nonatomic, retain) IBOutlet UIBarButtonItem *lastUpdateLabel;
- (IBAction)selectHome:(id)sender;
#end
Implementation file:
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 20.0f)];
label.text = #"last updated...";
label.textColor = [UIColor colorWithWhite:1.0 alpha:1.0];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentCenter;
label.font = [UIFont boldSystemFontOfSize:13.0];
label.userInteractionEnabled = NO;
lastUpdateLabel = [[UIBarButtonItem alloc] initWithCustomView:label];
[label release];
[toolbar setItems:[NSArray arrayWithObject:lastUpdateLabel]];
[self.view addSubview:self.navigationController.view];
//[self.view addSubview:toolbar];
//[self.navigationController.view addSubview:toolbar];
[self.navigationController.view setFrame:self.view.frame];
}
Any help is greatly appreciated!
EDIT:
I removed whatever I had in the nib which would cause the toolbar to appear/be modified and I updated my code in viewDidLoad to the following:
self.navigationController.toolbarHidden = NO;
//creating label in tool bar
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 20.0f)];
label.text = #"last updated...";
label.textColor = [UIColor colorWithWhite:1.0 alpha:1.0];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentCenter;
//label.highlightedTextColor = [UIColor colorWithWhite:0.5 alpha:1.0];
//label.highlighted = YES;
label.font = [UIFont systemFontOfSize:13.0];
label.userInteractionEnabled = NO;
UIBarButtonItem *lastUpdateLabel = [[UIBarButtonItem alloc] initWithCustomView:label];
//[lastUpdateLabel initWithCustomView:label];
//[label release];
//[toolbar setItems:[NSArray arrayWithObject:lastUpdateLabel]];
[self setToolbarItems:[NSArray arrayWithObject:lastUpdateLabel]];
And I end up getting a blank toolbar showing up. I fire up the debugger and this is what I see:
Aha! lastUpdateLabel's view's _text field is out of scope! But why? And how would I remedy this?
EDIT 2:
I have been able to add labels and an NSActivityIndicator with the following code:
#synthesize refreshDataButton;
//...
self.navigationController.toolbarHidden = NO;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20.0f, 0.0f, 80.0f, 40.0f)];
label.text = #"last updated...";
label.textColor = [UIColor colorWithWhite:1.0 alpha:1.0];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentCenter;
label.font = [UIFont systemFontOfSize:13.0];
label.userInteractionEnabled = NO;
[self.toolbar addSubview:label];
// create activity indicator
// dist frm lft, dist frm top
CGRect frame = CGRectMake( 90.0, 11.0, 25.0, 25.0);
UIActivityIndicatorView *loading = [[UIActivityIndicatorView alloc] initWithFrame:frame];
loading.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
[loading sizeToFit];
loading.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin);
[loading startAnimating];
[self.toolbar addSubview:loading];
But when I try to add a UIBarButtonItem I have no luck (doesn't show up in the toolbar):
self.refreshDataButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:100 target:self action:#selector(refreshDataButtonTapped)];
[self setToolbarItems:[NSArray arrayWithObject:refreshDataButton]];
Here is the header file:
#import <UIKit/UIKit.h>
//#import <CoreData/CoreData.h>
#interface ParkingRootViewController : UIViewController {
UINavigationController *navigationController;
UIToolbar *toolbar;
UIBarButtonItem *refreshDataButton;
//UIActivityIndicatorView *loading;
}
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
#property (nonatomic, retain) UIBarButtonItem *refreshDataButton;
//#property (nonatomic, retain) IBOutlet UIActivityIndicatorView *loading;
#property (nonatomic, readonly) NSString *applicationDocumentsDirectory;
-(IBAction)selectHome:(id)sender;
-(void)testCoreData;
-(void)refreshDataButtonTapped;
#end
The code should work...there are couple of suggestions I could make. Instead of:
[toolbar setItems:[NSArray arrayWithObject:lastUpdateLabel]];
try this:
[toolbar setItems:[NSArray arrayWithObject:lastUpdateLabel] animated:YES];
Also, since you are using a UINavigationController, NavControllers come with their own toolbar that you could use if you like. By default it is hidden, which you can make visible by doing this:
self.navigationController.toolbarHidden = NO;
and you can set its toolbar items by doing this:
[self setToolbarItems:[NSArray arrayWithObject:lastUpdateLabel]];
Hope this tid bit helps you. good luck!
The buttons need to be added by the viewController that is pushed by the navigationController. The navController holds the bar, but the items on the bar are controlled (added) by the viewController that is being shown. That way, each different kind of vc has it's own bar.
So take that barbuttonitem code, and plunk it in the init section of the vc, and enjoy.
//try this one.
- (void)viewDidAppear:(BOOL)animated
{
[toolbar setItems:[NSArray arrayWithObject:lastUpdateLabel]];
}
The code posted works fine, I think it has to be how the XIB is hooked up. I would re-do your connections in IB (ie break all the connections and re make them), save your XIB and try again.
You can add label to tool bar directly if u have instantiated it with frame...for example
UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(7,7,200,30)];
[lbl setBackgroundColor:[UIColor clearColor]];
[lbl setText:#"Test lbl"];
[_toolBar addSubview:lbl];