undeclared identifier on xcode - iphone

I just got to start developing for ios 6 on xcode.
But as a novice developer, i have come into a problem.
Following the guide in the book 'beginning ios5 development: exploring the ios sdk' on chapter 3, the 'Button fun' example.
I am having problems with the identifier 'statusText' which i have already declared in the .h code.
Here's my code so far, any help would be highly appreciated. thank you in advance.
my BIDViewController.h is like so
#import <UIKit/UIKit.h>
#interface BIDViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *
statusText;
- (IBAction)buttonPress:(UIButton *)sender;
#end`
and my BIDViewController.m is like so
#import "BIDViewController.h"
#interface BIDViewController ()
#end
#implementation BIDViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)buttonPress:(UIButton *)sender {
NSString *title = [sender titleForState:<#(UIControlState)#>];
statusText.text = [NSString stringWithFormat:#"%# button pressed.", title];
}
#end
i have followed the book but can't seem to understand why this occurs, pls help.

Well, for starters, this should be only one line
#property (weak, nonatomic) IBOutlet UILabel *statusText;
Now, when you declare a property, you don't get a variable named like that unless you synthesize it like this:
#implementation Class
#synthesize propertyName;
#end
That's why statusText doesn't exist.
You have three options:
Synthesize statusText like that so you can use that ivar.
instead of accessing the var directly, access the property like this self.statusText.
XCode is creating it's own variable, it's probably named _statusText, access it like that to access the variable directly.
You can also use a combination of 2 and 3

I am using the same book and had that problem, too. I have learned to use an underscore in the .m file
- (IBAction)buttonPressed:(UIButton *)sender {
NSString *title = [sender titleForState:UIControlStateNormal];
**_statusText**.text = [NSString stringWithFormat:#"%# button pressed.", title];
}

Related

IOS setting a custom delegate

I'm working through the IOS HelloWorld example and I have a question regarding setting the delegate for a TextField. In the example it was as easy as control-dragging from the TextField to the ViewController. But now say I wanted to create a custom class to act as my delegate as so:
#import <Foundation/Foundation.h>
#interface SweetAssDelegate : NSObject <UITextFieldDelegate>
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField;
#end
#import "SweetAssDelegate.h"
#implementation SweetAssDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField
{
NSLog(#"Calling Delegate");
[theTextField resignFirstResponder];
return YES;
}
#end
How can I set this class to be the delegate of the TextField? As far as I can tell there is not way to accomplish this through the GUI. I tried manually setting the delegation after window load with no success:
#import "ViewController.h"
#import "SweetAssDelegate.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UITextField *inputField;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
SweetAssDelegate *foo = [[SweetAssDelegate alloc] init];
[self.inputField setDelegate:foo];
NSLog(#"Delegate: %#", self.inputField.delegate);
}
I actually receive some sort of memory exception when bringing up the keyboard? Any ideas? Thanks.
As a side question, where should I always use viewDidLoad to initialize any variables? I noticed that init was not being called???
Your delegate object, foo, is allowed to fall out of scope and is released at the end of viewDidLoad and by the time the keyboard comes up, it doesn't exist anymore. Make it an ivar (or property) of your view controller, or otherwise make sure that foo doesn't fall out of scope at the end of viewDidLoad.
Thus, it could be something like:
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UITextField *inputField;
#property (strong, nonatomic) SweetAssDelegate *foo;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
self.foo = [[SweetAssDelegate alloc] init];
[self.inputField setDelegate:self.foo];
NSLog(#"Delegate: %#", self.inputField.delegate);
}
Your textfield delegate must have the implemented to be your textfield delegate I guess.
A delegate manages the communication between objects, which means your custom delegate must allow communication between objects and must provide methods, the textfield can work with...
Another example is a tableView:
You can make a custom delegate which implements the delegates and then calls some tableview related Methods...
Here this code might be interesting for you:
#interface myCustomDelegateForTextFields <UITextFieldDelegate>
#end
#implementation myCustomDelegateForTextFields
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
return TRUE;
}
#end
#implementation ViewController
myCustomDelegateForTextFields *txtfielddelegate = [[myCustomDelegateForTextFields alloc] init];
UITextField *whatever;
whatever.delegate = txtfielddelegate;
//your textfield now listens to the BOOL method in your custom delegate
#end
Is it that what u were looking for? :)
you can ofc pack the myCustomDelegateForTextField delegate in another class and call the class

NSDictionary setting to nil when passed to another class (IOS)

I am passing an NSDictionary object from one view class to another as I transition from a table view to a normal view to show details:
Passing Controller:
[tweetController setTweet:tweet];
Receiving Controller.h:
#interface TweetViewController : UIViewController {
NSDictionary *tweet;
}
#property (nonatomic, retain) NSDictionary *tweet;
Receiving Controller.m:
#implementation TweetViewController
#synthesize tweet = _tweet;
I then try to use this information to set the properties of some fields in my view:
- (void)viewDidLoad
{
[super viewDidLoad];
tweetLabel.text = [_tweet objectForKey:#"text"];
}
The result is a blank label and if I inspect the value of _tweet at this stage it is nil.
I originally had a method which set the value of tweet which I called at the same location as I am now setting the value. If I inspected the value at this stage it was fine.
I presume that the automagic setter through #synthasize is working, but somewhere else the value is being lost.
Sorry this is my first objective C anything! Thanks for any help in advance.
You are using your "tweet" instance variable, whereas the "tweet" property is synthesized to the "_tweet" variable.
You are probably calling the setTweet method after viewDidLoad executes.
I usually pass this kind of thing into a custom init method.
Alternatively, you could do the set before pushing the detail VC onto the nav stack.
Are you sure that tweetLabel isn't nil?
I've made a few corrections & optimisations to your code. You don't need to declare ivars in the header file anymore, they are generated automatically by #synthesize
- (void)dealloc; is only needed if you're not using ARC.
//.h
#interface TweetViewController : UIViewController
#property (strong, nonatomic) NSDictionary *tweet;
#property (strong, nonatomic) IBOutlet UILabel *tweetLabel
#end
//.m
#implementation TweetViewController
#synthesize tweet = _tweet;
#synthesize tweetLabel = _tweetLabel;
- (void)viewDidLoad {
[super viewDidLoad];
self.tweetLabel.text = [self.tweet objectForKey:#"text"];
}
- (void)dealloc {
[_tweet release];
[_tweetLabel release];
[super dealloc];
}
#end
Note: strong is equivalent to retain
To expand on #Rayfleck's answer, since you are new to Objective-C, your custom init method could look like this:
In TweetViewController.h:
- (id)initWithTweet:(NSDictionary*)tweet;
In TweetViewController.m:
- (id)initWithTweet:(NSDictionary*)tweet
{
self = [super init];
if (self) {
_tweet = tweet;
}
return self;
}
and then in your passing controller you'd allocate and initialize like this:
TweetViewController *tvc = [[TweetViewController alloc] initWithTweet:myTweet];

iOs simple hello, name not working

I have recently started to learn Obj-C and Iphone development basically through Lynda.com iOs SDK essentials course. But it was written for Xcode 3.x and I have 4.0.x installed, so things are different.
Basically, I take example from there and it just doesn't work for me and I can't figure it out being a noob to it.
//basicViewController.h
#import <UIKit/UIKit.h>
#interface basicViewController : UIViewController {
IBOutlet UITextField *txtName;
IBOutlet UILabel *lblMessage;
}
#property (nonatomic, retain) IBOutlet UITextField *txtName;
#property (nonatomic, retain) IBOutlet UILabel *lblMessage;
- (IBAction) doSomething;
#end
And my basicViewController.m
#import "basicViewController.h"
#implementation basicViewController
#synthesize txtName;
#synthesize lblMessage;
- (IBAction) doSomething
{
NSString *msg = [[NSString alloc] initWithFormat:#"Hello, %#",txtName.text];
[lblMessage setText:msg];
[msg release];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
I haven't changed basicAppDelegate.h and .m from what they were created like. I think I will post them anyway.
//
// basicAppDelegate.h
#import <UIKit/UIKit.h>
#class basicViewController;
#interface basicAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
basicViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet basicViewController *viewController;
#end
//
// basicAppDelegate.m
#import "basicAppDelegate.h"
#import "basicViewController.h"
#implementation basicAppDelegate
#synthesize window;
#synthesize viewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
#end
And here's my main.m:
//
// main.m
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
Also, on the Interface Builder window I have linked through File's Owner: label to lblMessage, TextField to txtName and doSomething to Button.
Now, the problem is: actually, the thing kinda works, only if I use the popup keyboard, not my physical one. And only if I type less than 3-4-5 symbols(differs sometimes).
If I use my keyboard, not the popping one - it gives me Thread 1 received signal EXC_BAD_ACCESS.
Same if I type too many symbols.
And I don't see much in All Output - just some usual stuff, the only suspicious line is:
warning:unable to compile regular expression "dyld"
Current language: auto; currently objective-c
(gdb)
So, guys, any kind help is appreciated.
First, there didn't change something since Xcode 3 except the interface.
Try to set a breakpoint and look where you app is crashing...
Try to delete the IBOutlets and create it new...
Try to create a new app...
If all not works, take your Mac and throw it outside a window, bring it back to Apple. (or install Xcode again --> much cheaper..)
I had the exact same problem and spent AGES looking for the solution. I found out how to make it work and I've made a tutorial on how to make the Hello, Name application on XCode. Take a look and see if it helps. I think if you follow the steps one by one you should have your app running in no time.
http://techtalktone.wordpress.com/2011/11/26/hello-world/
Hope this helps ;)

Facebook API causes app to crash when switching back to main view

Heads Up: I'm not too comfortable with Objective C to know exactly what I'm talking about..
Here's my life story:
My app basically consists of 3 views: main, facebook, and twitter.. No problems with the twitter, no problems switching back and forth between views until... bum bum bum.. I started using the Facebook API in guidance from this site: http://www.mobisoftinfotech.com/blog/iphone/iphone-fbconnect-facebook-connect-tutorial/
Now I can connect to FB and use their API and post without any problem, but when I switch back from the Facebook View on my app to the main view, it switches and then immediately crashes..
FacebookViewController.m
#import "FacebookViewController.h"
#import "Crush_LoveAppDelegate.h"
#define _APP_KEY #"43e37a535cc09c2013bd76fde78dfcc7"
#define _SECRET_KEY #"cc14801521a0c4d1dc31b7cacb891072"
#implementation FacebookViewController
#synthesize facebookFeed;
#synthesize delegate;
#synthesize loginButton;
#synthesize facebookAlert;
#synthesize usersession;
#synthesize username;
#synthesize post;
- (void)viewDidLoad {
Crush_LoveAppDelegate *appDelegate =
(Crush_LoveAppDelegate *) [[UIApplication
sharedApplication]delegate];
if (appDelegate._session == nil){
appDelegate._session = [FBSession sessionForApplication:_APP_KEY secret:_SECRET_KEY delegate:self];
}
if(self.loginButton == NULL)
self.loginButton = [[[FBLoginButton alloc] init] autorelease];
loginButton.frame = CGRectMake(110, 200, 100, 50);
[self.view addSubview:loginButton];
[super viewDidLoad];
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
}
- (IBAction)done:(id)sender {
[self.delegate facebookViewControllerDidFinish:self];
}
FacebookViewController.h
#import <UIKit/UIKit.h>
#import "FBConnect/FBConnect.h"
#import "FBConnect/FBSession.h"
#import "FBConnect/FBRequest.h"
#import "FBConnect/FBStreamDialog.h"
#protocol FacebookViewControllerDelegate;
#interface FacebookViewController : UIViewController <UIApplicationDelegate, FBSessionDelegate, FBRequestDelegate>{
IBOutlet UIWebView *facebookFeed;
id <FacebookViewControllerDelegate> delegate;
FBLoginButton *loginButton;
UIAlertView *facebookAlert;
FBSession *usersession;
NSString *username;
BOOL post;
}
#property(nonatomic,retain) FBLoginButton *loginButton;
#property(nonatomic,retain) UIAlertView *facebookAlert;
#property(nonatomic,retain) FBSession *usersession;
#property(nonatomic,retain) NSString *username;
#property(nonatomic,assign) BOOL post;
#property (nonatomic, assign) id <FacebookViewControllerDelegate> delegate;
#property (nonatomic, retain) IBOutlet UIWebView *facebookFeed;
- (IBAction)done:(id)sender;
#end
#protocol FacebookViewControllerDelegate
- (void)facebookViewControllerDidFinish:(FacebookViewController *)controller;
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
-(void)getFacebookName;
-(void)postToWall;
#end
I chopped some of the .m off of the post to save space, but you get the idea.. I've narrowed it down and it seems like the problem is caused during this line in .m
appDelegate._session = [FBSession sessionForApplication:_APP_KEY secret:_SECRET_KEY delegate:self];
I've been trying to debug it myself for a few hours and I don't know enough on my own to diagnose it myself..
Any thoughts?
Im not sure why your app is crashing, but thought I'd mention that there are a few really good Apple WWDC videos about bug checking and crashing in XCode. In particular, "Debugging with Xcode 4 and LLDB" and "Understanding Crash Reports on iPhone OS", both from the WWDC 2010 videos. I believe you need a developer login to access these videos however but certainly worth a look if you're interested in learning more about debugging.
Okay, for anyone else who had this issue, here's what I did..
FacebookViewController.m File
- (void)dealloc {
[username release];
[usersession release];
[loginButton release];
[super dealloc];
}
I commented out the [loginButton release]; line and it worked oddly enough.. I don't know if this will give me serious problems down the road, but it works for now..

How to pass a string from one view to another in tab based app

I have created a tab based application having 4 tabs and 4 views respective to these tabs.
I have a string in first view and when I printing this string in second view it printing null.
In first view.h
NSString *dateString;
#property(nonatomic,retain) NSString *dateString;
In first view.m
#synthesize dateString;
dateString=button6.titleLabel.text;
NSLog(#"dateString:%#",dateString);
In second view.h
NSString *dateString;
#property(nonatomic,retain) NSString *dateString;
In second view.m
#synthesize dateString;
- (void)viewDidLoad { [super viewDidLoad];
NSLog(#"dateString:%#",self.dateString);
}
Add your view controllers as properties for the application delegate (if the app is a relatively simple design).
Then you can reference the properties of the second view controller from the first view controller, by way of the app delegate. (One such property could be the string you want the second VC to copy or retain.)
Create NSString variable in Application delegate class and set the Property and make synthesize that variable.
And set the #"" (blank) value in applicationDidFinishLaunching method.
For Example - my variable name is str, then initialize str in applicationDidFinishLaunching like self.str = [NSString stringWithFormat:#""];
And now you can use it in any tab *view* and set the value as per your require.
More code
AppDelegate.h
#interface AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
NSString *baseURL;
}
#property (nonatomic, retain) NSString *baseURL;
#end
AppDelegate.m
#import "AppDelegate.h"
#implementation AppDelegate
#synthesize window;
#synthesize baseURL;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
self.baseURL = [NSString stringWithFormat:#""];
}
- (void)dealloc
{
[baseURL release];
[window release];
[super dealloc];
}
#end
ViewController1.h
#class AppDelegate;
#interface ViewController1 : UIViewController <UITextFieldDelegate>
{
AppDelegate *appDelegate;
}
#end
ViewController1.m
#import "AppDelegate.h"
#import "ViewController1.h"
#implementation ViewController1
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
NSLog(#"value - %#",appDelegate.baseURL); // Here you can set or get the value.
}
it may not be the best answer.but creating a string variable in the appdelgate and passing the variable to this from the first view and fetching it from the second view works for me
Really, did we lose focus of MVC and the most awesome of abilities that is easy to do in iPhone Development?
How about a delegate?
#protocol ViewOneDelegate
- (void)getStringVariable;
#end
#interface ViewOneModel : NSObject
{
NSString* _stringVariable;
id<ViewOneDelegate> _theDelegate;
}
#property (nonatomic, retain) id<ViewOneDelegate> theDelegate;
#end
Assign a controller to be the delegate for the ViewOneModel.
Here is a simple solution, but not the best one, Create a global variable, and just use that.
Header
extern NSString *GlobalString;
#interface GlobalVariables : NSObject {
}
#end
implementation
#import "GlobalVariables.h"
#implementation GlobalVariables
NSString *GlobalString;
#end
And now to have access to the variable just import the header in the file you want to use.
You'll probably want to check if it's initiated before you use it.