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.
Related
#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
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
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
I've looked up about a dozen different pages, both on how to call methods from other views and instances when the method is being skipped, but no answer has come.
I'm going to be thorough here. Two relevant classes:
SpeakersView
and
GetData
GetData is just there because I'm going to need to call that method from half a dozen different classes, best to just write it once.
Here's GetData.h:
#import <UIKit/UIKit.h>
#interface GetData : UIViewController
-(NSArray *)getTableArray:(NSString *)section :(NSString *)entity;
#end
Here's GetData.m:
#interface GetData (){
NSArray *tableData;
NSArray *titleData;
NSArray *splitData;
}
#end
#implementation GetData
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSArray *)getTableArray:(NSString *)section :(NSString *)entity{
//Code omitted. Shouldn't really have anything to do with why the method is being skipped.
}
#end
So here's how I'm calling the method in SpeakersView.m:
GetData *GD;
menu_List = [GD getTableArray:#"Speakers" :#"John Smith"];
Recommendations? I should mention I've had no problem calling other methods in exactly the same way from different classes.
GetData *GD; doesn't initialize the UIViewController. It's nil at that point, so you cannot call a method on it.
Try:
GetData *GD = [[GetData alloc] init];
Also, why is GetData a UIViewController? Make it an NSObject, you don't need a whole view controller with all of its bulk if you're just using it for that method.
Calling a class "GetData" that inherits an UIViewController shows clearly a huge conception problem. A controller is a class that feed its views with the model (in the MVC pattern). It's not an utility class.
In your example you're also not instantiating the controller.
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