Programmatically focus on UITextView is not working - iphone

Before closing the post as duplicate,believe me,i am searching this for 2 days but still nothing.
I will try to be as clear as possible:
I have a view with a textview.I need to set focus automatically on the textview so the keyboard ill appear.
PostView.h
#interface PostView : UIViewController{
UITextView *txtPesto;
}
#property (nonatomic,retain) IBOutlet UITextView *txtPesto;
#end
PostView.m
#import "PostView.h"
#implementation PostView
#synthesize txtPesto;
- (void)viewDidLoad
{
[super viewDidLoad];
[txtPesto becomeFirstResponder];
}
For some very strange reason my code is not working,although i have tried many samples and different approaches.
Any help?

Answer from my comment (for closing question):
Right click on textView, and "new referncing outlet", maybe that will be helpful?

You are declaring txtPesto twice in your .h file:
#interface PostView : UIViewController{
UITextView *txtPesto;
}
#property (nonatomic,retain) IBOutlet UITextView *txtPesto;
#end
When you declare it as a property, you don't need to do it again. So remove the extra declaration, and just use this:
#interface PostView : UIViewController
#property (nonatomic,retain) IBOutlet UITextView *txtPesto;
#end
I don't see any other reason that becomeFirstResponder wouldn't work.

Related

Missing '#end' // #end must appear in an objective-c context

This is my code:
#import <UIKit/UIKit.h>
#interface CustomCellArticle: UITableViewCell
#property(nonatomic,retain) IBOutlet UILabel *name;
#end
In first time I received this error:
Missing #end
Expected identifier or '('
in the first of the code, and it required me to add #end in the first to fix it.
the code became like this:
#import <UIKit/UIKit.h>
#end //here the seconde error
#interface CustomCellArticle: UITableViewCell
#property(nonatomic,retain) IBOutlet UILabel *name;
#end
When I add it, I received a new error:
#end must appear in an Objective-C context
I don't know what's happened exactly, please help!
I used the same class in another project and it works fine!
Yes that is from a another header or implementation file already imported beforehand that is missing a #end
It could be a .h or .m file
#end should only come once in a single file.Whats with the top #end.And import all files at the top ?
you may have opened a "{" that is never closed with a "}" before the #end line...
so, the error is not at the #end line... but xcode just find out you are missing a "}" or ")"
Let me show whole file .h and .m file then i can answer well No Problem.
You need to remove first #end from .h file and run you will solve the issue.
Instead of this :
#import <UIKit/UIKit.h>
#end
#interface CustomCellArticle: UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *name;
#end
Use this :
#import <UIKit/UIKit.h>
#interface CustomCellArticle: UITableViewCell
{
}
#property (strong, nonatomic) IBOutlet UILabel *name;
#end
May this will help you. This is working fine for me.
I have checked also in xcode. You Just need to remove first #end.
If this thing is not working you have some another issue.

How to add a popup UIView (of a customized class) on a UIViewController

I'm new on this, and I would like to get some advice because I don't know what I'm doing wrong.
I want to make an app in xcode, with a UIView with some items, and when you do something, another UIView (smaller than the first) pops up above the first UIView. The popup UIView would be a customized class.
I have started with the UIViewController template and the initial UIView, and I have linked all the items in the .storyboard, and it works. But when I create my own UIView class (from objective-C class), put the second UIView over the first in the storyboard and link it to my class, something goes wrong.
The UIView appears, but when I try to set it to hidden, it doesn't answer. It's like it's not receiving the messages, so I think I don't link it well programmatically and just appears because of the storyboard.
I don't know if I have to create another UIViewController instead of the UIView, or if this is the correct path.
Can anybody explain me a little, or just write a little code snippet with the instantiation of the second view and adding it?
Lots of thanks!!
(I paste some code, of the declaration in .h and instantiation in .m)
#import <UIKit/UIKit.h>
#import "EditView.h"
#interface ReleaseViewController : UIViewController <UIWebViewDelegate, UISearchBarDelegate> {
IBOutlet UIWebView *web;
IBOutlet UISearchBar *search;
IBOutlet EditView *evHack;
}
#property (nonatomic, retain) IBOutlet UIWebView *web;
#property (nonatomic, retain) IBOutlet UISearchBar *search;
#property (nonatomic, retain) IBOutlet EditView *evHack;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
search.delegate = self;
web.delegate = self;
evHack = [evHack initWithFrame:CGRectMake(0, 44, 320, 377)];
[evHack setHidden:YES];
}
EditView Class (I still have nothing):
#import <UIKit/UIKit.h>
#interface EditView : UIView
#end
#import "EditView.h"
#implementation EditView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSLog(#"View created");
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
initWithFrame only works when you alloc/init an app. If its already initialized, in this case by the storyboard, just set its frame:
evHack.frame = CGRectMake(0,44, 320, 377);
I don't know what it looks like in IB, But setting its frame in code may be redundant if you set it in IB too. To check whether evHack is hooked up right, NSLog evHack in viewDidLoad. If you get nil back, it's not hooked up right.

cannot find protocol declaration custom protocol delegate iphone

Slowly but surely getting this delegation and protocol stuff on iphone but I cannot understand this error.
I have declared my protocol in my first viewcontroller.
In the second viewcontroller i try to add it at the top after i have imported it into the header file and it cannot find it. see my code below.
//SendSMS
#import <UIKit/UIKit.h>
#import "LoginPage.h"
#import "MessageOptions.h"
#protocol SMSProtocol <NSObject>
-(NSString *)postbackType;
#end
#interface SendSMS : UIViewController <UITextViewDelegate, UITextFieldDelegate> {
id<SMSProtocol> delegate;
MessageOptions *messageOptions;
LoginPage *loginPage;
IBOutlet UITextField *phonenumber;
IBOutlet UITextView *smsBody;
IBOutlet UIScrollView *scrollview;
}
#property (nonatomic, retain) id<SMSProtocol> delegate;
-(IBAction)LoadMessageOptions;
#end
Then my second view
#import <UIKit/UIKit.h>
#import "SendSMS.h"
#interface ScheduledSMS : UIViewController <SMSProtocol>{
}
-(IBAction)popBack;
#end
That is surely strange. Have you tried restarting Xcode? Xcode has a habit of not indexing symbols for me when I add new files.
You should also look into how your naming conventions. SendSMS is not really a good class name, more of a action method name. I would go for SendSMSViewController, since that is what it is.
By that it would follow that SMSProtocol should be named SendSMSViewControllerDelegate, since that is what it is.
Methods in a delegate protocol should contain the sender and one of the three words will, did, or should. If not at the very least it should name what it expects to return. -(NSString *)postbackType; should probably be -(NSString *)postbackTypeForSendSMSViewController:(SendSMSViewController*)controller;.

iPhone Simulator Text Box Takes up Whole Screen

Here is my code. Not sure what's awry.
controller.h
#import <UIKit/UIKit.h>
#interface ch4iOSPracticeViewController : UIViewController {
UITextField *nameField;
UITextField *numberField;
}
#property (nonatomic, retain) IBOutlet UITextField *nameField;
#property (nonatomic, retain) IBOutlet UITextField *numberField;
#end
controller.m
#import "ch4iOSPracticeViewController.h"
#implementation ch4iOSPracticeViewController
#synthesize nameField;
#synthesize numberField;
delegate.m Everything properly released.
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
When you add a textField to the iPhone using interface builder, I am guessing that there was another view already present there; So when you pulled the textField over it, it had stretched. Try pulling it inside a plain View.
It is either that you pulled a textview instead of a textfield.

Passing object from controller to a view

I'm following iPhone dev courses from Stanford Open-University, and I've been blocked for 2 days on assignment3, maybe someone can help me here?
The tasks are:
Create a custom UIView subclass that will display your PolygonShape object
Give your view class access to the PolygonShape object so that it can retrieve the details of the polygon as needed
The problem is: how do I give my view class access to the polygon object defined in my controller?
Here is my implementations if it can help:
CustomView.h:
#import "PolygonShape.h"
#interface CustomView : UIView {
IBOutlet PolygonShape *polygon;
}
- (NSArray *)pointsForPolygonInRect:(CGRect)rect numberOfSides:(int)numberOfSides;
#end
Controller.h:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "PolygonShape.h"
#import "PolygonView.h"
#interface Controller : NSObject {
IBOutlet UIButton *decreaseButton;
IBOutlet UIButton *increaseButton;
IBOutlet UILabel *numberOfSidesLabel;
IBOutlet PolygonShape *polygon;
IBOutlet PolygonView *polygonView;
}
- (IBAction)decrease;
- (IBAction)increase;
- (void)awakeFromNib;
- (void)updateInterface;
#end
And after you figure it out, it might not hurt to touch up on some objective-c basics:
http://www.cocoacast.com/?q=node/103
Found my own answer, I missed a setPolygon method in my CustomView to link both... stupid...
in CustomView.h:
#import "PolygonShape.h"
#interface CustomView : UIView {
IBOutlet PolygonShape *polygon;
}
#property (readwrite, assign) PolygonShape *polygon;
- (NSArray *)pointsForPolygonInRect:(CGRect)rect numberOfSides:(int)numberOfSides;
#end
in CustomView.m:
#implementation CustomView
#synthesize polygon;
...
#end
in Controller.m:
- (void)awakeFromNib {
// configure your polygon here
polygon = [[PolygonShape alloc] initWithNumberOfSides:numberOfSidesLabel.text.integerValue minimumNumberOfSides:3 maximumNumberOfSides:12];
[polygonView setPolygon:polygon];
NSLog (#"My polygon: %#", [polygon description]);
}
I just finished assignement 3 last night. I solved this connection all in Interface Builder. First I created an outlet on the "PolygonView" UIView subclass for the PolygonShape and then connected it to the instance of the Polygon model. From what I have read in the Google Group and on various other sites, I do not think there is one right way to connect this UIView to the model and the controller. But it worked I think there is nothing wrong with the View knowing about the model.
So why aren't you declaring them as properties of the class?