I'm using Xcode coding an ios7 app, whenever I try to make a text view it is editable by the user. How can I fix this so it can not be editable? It is editable in the iPhone simulator, what property do I need to change to disable this and where can I find it?
If you click on the textView in the storyboard (if you created it in the storyboard), then you can open the attributes inspector on the right and there is a checkbox that says editable. If it was not created in the storyboard, then there is a property
textView.editable = NO;
Try:
//.h
#interface YourViewController:UIViewController<UITextViewDelegate>
#property (strong, nonatomic) IBOutlet UItextView *textView;
#end
//.m
-(void) viewDidload
{
_textView.delegate =self;
}
- (BOOL)textViewDidBeginEditing:(UITextField *)textField
{
return NO;
}
If you are using storyboard, click on UITextField in storyboard and there is a property called behaviour editable: remove the tick than and build.
Related
I user Interface Builder to position a UIButton and a UIImageView superimposed.
In the code, I change button label if image exists
In Example.h
#property (strong, nonatomic) IBOutlet UIButton *takePicture;
#property (strong, nonatomic) IBOutlet UIImageView *image;
In Example.m
[self.image setImage:aPhoto];
(...)
NSString *pictureButtonTitle = myCondition#"Changer la photo":#"Ajouter une photo";
(...)
[self.takePicture setTitle:pictureButtonTitle forState:UIControlStateNormal];
I use this code for a view, and I see "Change picture" correctly on my picture when myCondition is true.
But in another view, nothing appears !! WHY ?
You could either do it in the interface builder - as you did, or do it in your viewDidLoad method:
- (void)viewDidLoad {
[super viewDidLoad];
[self.view bringSubviewToFront:self.button];
}
Arranging the outlets in the IB as you did mean that the lower object will be on top of the others... The button will be "front"/on top when it is placed below all other objects as you shown in the screenshot.
If you wish to arrange the outlets differently, you could use these methods as well:
[self.view insertSubview:self.button aboveSubview:self.imageView];
Or:
[self.view insertSubview:self.imageView belowSubview:self.button];
Well, I found out : in Interface Builder, it seems that the order you place your objects matters
If you place an UIImageView before UIButton, it works well.
I see the label
But if you do the reverse : button is under the image.
And the label is not displayed
I still don't know if it's the only way to rearrange these items ?
I'm having a hard time implementing a simple scroll on my detail view.
The app is straightforward with a Master and Detail views.
When the user taps an item on Master, the Detail view is pushed with larger photo, blog text and etc.
I would like the entire Detail view to scroll, so if the picture is tall, or the text is long, they can scroll vertically to see/read more. I do not want the user to scroll these items individually. It should feel like webpage scrolling.
Currently my Detail view loads OK but I can't make it scroll.
My DetailViewController.h
#import <UIKit/UIKit.h>
#interface DetailViewController : UIViewController {
IBOutlet UILabel *postTextLabel; // wired to Text Label
IBOutlet UILabel *postAuthorNameLabel; // wired to Author Label
}
#property (strong, nonatomic) id detailItem;
#end
My DetailViewController.m
#import "DetailViewController.h"
#interface DetailViewController ()
- (void)configureView;
#end
#implementation DetailViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self configureView];
}
- (void)configureView
{
if (self.detailItem) {
NSDictionary *post = self.detailItem;
NSString *postText = [post objectForKey:#"post_text"];
NSString *postAuthorName = [post objectForKey:#"post_author_name"];
postTextLabel.text = postText;
postAuthorNameLabel.text = postAuthorName;
}
}
#end
Structure on IB:
Any ideas on what's missing to make this work?
I would do the following:
1)(optional)turn your View into a scrollview by dragging it into the view in the scruture list on the side.
2)link the scrollView into your viewcontroller .h and make an Outlet connection something like this
#property (strong, nonatomic) IBOutlet UIScrollView *scroller;
(make sure you add #synthesize in the .m if you add this manually)
and make sure it is connected in IB!
3)set the contentsize of the scrollview in viewDidLoad method
scroller.contentSize = CGSizeMake(320,550);
NOTE: IBOutlet UILabel *postTextLabel; should actually probably be a UITextView so you can access ContentSize
Which would allow for the following.
CGRect frame = postTextLabel.frame;
frame.size = postTextLabel.contentSize;
postTextLabel.frame = frame;
scroller.contentSize = CGSizeMake(320, frame.size.height+200);//200 or how ever much space is above the textView
and again that only works if you use a UITextView
for ipad replace 320 with 768 or 1024, or whichever depending on orientation
The best way to connect it in IB is like this holding down control and dragging to the .h file
and make sure it is set to automatic like in the picture, and pointing to> the .h of your view.
Adding it like this also automatically adds the #synthesize for you.
Make sure UserInteractionsEnabled are checked here for our scrollview
I have a login page that places the username above the password then the login button last on the bottom. When I type the username, the keyboard covers the pass and login. How do I friggin close the keyboard when someone clicks outside of the text fields or is there a way to generate a close keyboard button? The only ways I have found how to do it is to programmatically add fields to the UI. Is there a way to accomplish this using the editor?
Here is my header file:
#interface Login : UIViewController{
RootViewController *rootViewController;
IBOutlet UITextField *usernameField;
IBOutlet UITextField *passwordField;
IBOutlet UIButton *loginButton;
}
#property (nonatomic, retain) UITextField *usernameField;
#property (nonatomic, retain) UITextField *passwordField;
#property (nonatomic, retain) UIButton *loginButton;
#property (nonatomic, retain) RootViewController *rootViewController;
- (IBAction) login: (id) sender;
#end
The only way I found to do it was something like:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (void)loadView
{
self.view = contentView;
[contentView release];
// Create a field with a Done return key
usernameField = [[[UITextField alloc] initWithFrame:CGRectMake(120.0f, 40.0f, 150.0f, 30.0f)] retain];
[usernameField setBorderStyle:UITextBorderStyleRoundedRect];
usernameField.placeholder = #"name";
usernameField.returnKeyType = UIReturnKeyDone;
usernameField.clearButtonMode = UITextFieldViewModeWhileEditing;
usernameField.delegate = self;
[contentView addSubview:usernameField];
[usernameField release];
}
Which programs the view and adds fields.Is there a way around this?
You can fix this in a number of ways. One way would be to slide the view containing your fields and button up when the keyboard becomes active by adjusting the frame top. Look at UITextFieldDelegate. If the text fields are in a table view then you can shrink the height of the table view.
If you want to hide the keyboard when a user taps outside the text field, you can add a UIGestureRecognizer to the view. When a tap is detected you can tell the active text field to resignFirstResponder.
The better approach is to listen for the UIKeyboard notifications and resize your view appropriately so the entire form is visible when the keyboard appears. From there you can wire up the return button on the keyboard to take an appropriate action depending on which text field has first responder status. For instance, pressing return from the username field will tab to the password field and pressing it from the password field would be equivalent to tapping the login button. No need to manually manage the keyboard.
Another approach is to have an inputAccessoryView for the UITextField with a "Done" button, that resigns the keyboard when clicked.
Put everything on a scroll view, and when the keyboard is firstresponder then add the height of the keyboard to the height of the scroll view. Do the inverse when the keyboard disappears.
Let me start off by saying that this is my first real Cocoa app. It's a simple app that pretty much displays my website in a borderless window. The way I'm currently creating a borderless window is using the following:
- (void) awakeFromNib {
[window setStyleMask:NSBorderlessWindowMask];
[window setAcceptsMouseMovedEvents:YES];
[window setMovableByWindowBackground:YES];
[window setLevel:NSNormalWindowLevel];
}
The problem with this is that as a result, the WebView within the window does not pass mouse over events to elements on the loaded page, nor does it provide the ability to type in text fields. I know that I'm supposed to create a custom window instead and move the contentView into it but I'm too new to Objective-C to figure out how.
I've also tried declaring all of these with no luck:
#implementation specikAppDelegate
#synthesize window;
#synthesize webView;
- (BOOL) canBecomeKeyWindow { return YES; }
- (BOOL) canBecomeMainWindow { return YES; }
- (BOOL) acceptsFirstResponder { return YES; }
- (BOOL) becomeFirstResponder { return YES; }
- (BOOL) resignFirstResponder { return YES; }
...
#end
Additionally, I'd like to be able to move the window by clicking and dragging it anywhere but that's a side thought. I've searched extensively online, and cannot find a solution to this.
Contents of my .h file (just in case):
#interface specikAppDelegate : NSObject <NSApplicationDelegate> {
IBOutlet NSWindow *window;
IBOutlet WebView *webView;
}
#property (assign) IBOutlet NSWindow *window;
#property (nonatomic, retain) IBOutlet WebView *webView;
- (IBAction)openAboutPanel:(id)sender;
#end
Any help would be appreciated, and like I said, I'm super new to the world of Objective-C and Cocoa, but I do come from a PHP development background.
As explained in this answer, windows without title or resize bar (including borderless windows) cannot become key windows.
You were right about overriding -canBecomeKeyWindow, but you’ve missed the correct place. You shouldn’t do it in your application delegate. You need to create an NSWindow subclass and then override that method.
This sample code of apple should give you the information you need, its really easy to change the way it works and change it into your own drawn NSWindow ( without a border :D )
http://developer.apple.com/library/mac/#samplecode/RoundTransparentWindow/Introduction/Intro.html
I need to set the labels of the UI dyanmically..
I want be read the text from an xml file and would like to set the text to the controls in the NIB.
I guess i can recognise the conrol by using the TAG attribute of the control.
Now i would like to get all the objects in the window(controls in the Nib) into an array?
Please suggest me on this.
In your code you'll want to create a link to your control. In xcode, in your .h file put something like:
#interface Mycontroller : UIViewController {
IBOutlet UILabel *namelabel;
}
#property (nonatomic, retain) IBOutlet UILabel *namelabel;
-(void)ChangeName:(NSString *)toName;
#end
Then in your .m file put something like:
#implementation ProjectCell
#synthesize namelabel;
-(void)ChangeName:(NSString *)toName {
[namelabel setText:#"your new string"];
}
You then want to open your nib in interface builder. Select your label and go to the inspector (Tool Menu > Inspector). Go to the Connections tab (blue circle w/ white arrow, and then click and drag the circle by New References Outlet from there to File's Owner in the nib window. Select "namelabel" from the popup. They're now linked and changing namelabel in code will change that specific label that you setup in interface builder.
I agree with the above soultion....
I had to set the title of each textfield and buttons in applicationdidFinishLaunching function.