Hello World Objective-C Issue - ios

I have recently started looking into basic IOS app development I am using the following tutorial online https://www.youtube.com/watch?v=GHK3oREwVls&list=PLhAWmh1PlbzGrr8PGLvJBtJ7qniLVTQ9E and I seem to have hit a stumbling block, below are my two view controllers. The error I am getting is regarding the displaytext.text section of the view controller M. The error appearing is;
HelloCocoaViewController.m:30:4: Use of undeclared identifier 'displaytext'; did you mean '_displaytext'?
When I make the suggested change I get a Thread 1: signal SIGABRT around the below code.
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([HelloCocoaAppDelegate class]));
Below is my the code I am currently using.
//
// HelloCocoaViewController.h
// Test
//
#import <UIKit/UIKit.h>
#interface HelloCocoaViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *displaytext;
- (IBAction)hellobutton:(id)sender;
- (IBAction)byebutton:(id)sender;
#end
//
// HelloCocoaViewController.m
// Test
//
//
#import "HelloCocoaViewController.h"
#interface HelloCocoaViewController ()
#end
#implementation HelloCocoaViewController
- (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)hellobutton:(id)sender {
displaytext.text=#"Hello";
}
- (IBAction)byebutton:(id)sender {
displaytext.text=#"Bye";
}
#end
I will appreciate any and all help.
Edit: I have now tried to implement the solutions suggested (I hope this is correct) however I am sill experiencing the same SIGABRT issue.
#implementation HelloCocoaViewController
#synthesize displaytext;
- (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)hellobutton:(id)sender {
self.displaytext.text=#"Hello";
}
- (IBAction)byebutton:(id)sender {
self.displaytext.text=#"Bye";
}
#end

as you are not using #synthesize to define a backing variable for the property it will implicitly add on as _<propertyname>
so either use the property as self.displaytext or access the property's backing variable directly via _displaytext
Since Apple switched the default compiler from GCC to llvm Objective-C evolves very fast. WWDC videos are a great way to keep track of those improvements
WWDC 2013: Advances in Objective-C
WWDC 2012: Modern Objective-C

Related

not sure whats causing these errors

#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (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.
}
#end
I'm not sure what I changed or how to fix these errors:
Cannot find interface declaration for 'ViewController';
View controller cannot use 'super because it is not a root class
Likely you forgot to sub-class UIViewController (check your header):
#interface ViewController : UIViewController

Viewcontroller Dealloc Method Never Gets Called

I know there are many questions like this. I read all. My problem is very simple.
I created a single view app from xcode file>new project>single view app.
Then i added a second uiviewcontroller in storyboard and a new viewcontroller class named secondViewController. I dragged a button to main viewcontroller and by ctrl+drag to secondViewController on storyboard. I did the reverse in secondViewController. And just added dealloc functins with nslog to class files. I also added weak references to uibuttons
Dealloc method of each viewcontroller never gets called when view changes.
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"viewDidLoad 1");
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
NSLog(#"dealloc 1");
}
SeconViewController.m
#import "SecondViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
NSLog(#"dealloc 2");
}
#end
ARC is enabled.
Zombies seem to be disabled on product>edit scheme. I am using xcode 6.2. In instruments allocation screen memory rises at each toggle.
What is the problem, I couldnt find?
dealloc calls when object's (Here its viewcontroller object) swipe out from memory.But here in your case you must presenting view controllers from one another that leads to call only viewwilldisappear and diddisappear.
In storyboard if you want to remove those view controllers completely from memory u should call unwind segue

calling a method from same class in xcode

i want to call a method "shows()" but why i am getting the error that "expected identifier or ( " and "use of undeclared identifier self"
ViewController.m
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
NSDictionary *inventory;
}
- (void)shows;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
inventory = #{#"Rahul":[NSNumber numberWithInt:11],
#"iOS":[NSNumber numberWithInt:22]};
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)shows
{
NSLog(#"%#",inventory);
}
[self shows];
#end
You can't call [view shows]; in the class scope, you need to call it within a method, like in viewDidLoad
Call it like this:
- (void)viewDidLoad {
[super viewDidLoad];
inventory = #{#"Rahul":[NSNumber numberWithInt:11],
#"iOS":[NSNumber numberWithInt:22]};
// Do any additional setup after loading the view, typically from a nib.
[self shows];//SHOWS call moved here...
}
You can't call this method outside. You can call it inside of anyother method. Like you can call this method in ViewDidLoad.
- (void)viewDidLoad {
[super viewDidLoad];
[self shows];
}
You have to place the code in another method, it can't be left in the open by it self.
Put it in your viewDidLoad: method or elsewhere

How to give UIWebView access to current gps location

I have a UIWebView inside my app, this loads a website which is trying to get the current gps location of the user. On Safari it presents a popup which asks if you want to give your current location to the website, on my UIWebView no such thing happens and my website is not able to get the gps location of the user. How can I accomplish this? I am new to ios developing so bear with me if this is something very easy to do, but I couldnt find an example so far.
Here is what I have so far:
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
IBOutlet UIWebView *_webView;
}
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[_webView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:#"http://google.com"]]];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)goBack:(id)sender {
if ([_webView canGoBack]) {
[_webView goBack];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Edit your app's info.plist and set
NSLocationWhenInUseUsageDescription "String you want to explain why you need location."

adding input parameter to method makes it stop working

Lets say I have a very simple method in my ViewController that returns a number.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"Number: %i",self.returnNumber);
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)returnNumber
{
NSInteger number = 2;
return number;
}
#end
This works just fine, but when I modify the method returnNumber to accept an input parameter like so:
#import "ViewController.h"
#interface ViewController (
)
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"Number: %i",self.returnNumber:2);
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)returnNumber:(NSInteger)insertedNumber
{
NSInteger number = insertedNumber;
return number;
}
#end
The compiler says:
Property 'returnNumber' not found on object of type 'ViewController *'
Is it some kind of bug or did I totally fail to learn how Objective-C methods work?
Is it some kind of bug or did I totally fail to learn how Objective-C methods work?
The latter. You're confusing message send and property accessor notation. Property accessor methods can't take any arguments. What you want instead are:
I. [self returnNumber:2]
II. A good Objective-C beginner's guide, with special regards to syntax and properties. Here's Apple's official material on the subject for a starter.
Declare method in .h file.
-(NSInteger)returnNumber:(NSInteger)insertedNumber;
and call the method like this
[self returnNumber:2];
You have to put it in brackets like this
[self returnNumber:2]
That should fix the problem.
NSLog(#"Number: %i",[self returnNumber:2]);
try this for more about Methods and Messaging
[self returnNumber:2] will be the solution. As self defines its own class or viewController and to call a method of the same class we call that method using self.

Resources