calling a method from same class in xcode - ios

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

Related

iOS SDK - Track viewDidLoad method

I am working on a SDK, where I will have to make a call to our server whenever viewDidLoad method of any UIViewController is called in the app integrated with our SDK. I am trying to use Categories as shown below:
#import "DymmyViewController.h"
#interface DymmyViewController ()
#end
#protocol DummyDelgate;
#interface UIViewController (DummyAddition)
-(void)viewDidLoad;
#end
#implementation DymmyViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
#end
#implementation UIViewController (DummyAddition)
-(void)viewDidLoad{
//server call
}
#end
Nothing happens with this and I get a warning saying "Category is implementing a method which will also be implemented by its primary class"
I understand this is not the way to get this done. Is there any other way I can do this? May be using NSNotification?
This is the way to use protocols and delegates. Delegated are good techniques to respond from one class to another class.
DummyView.h
#import <UIKit/UIKit.h>
#class DummyView;
#protocol DummyViewDelegate <NSObject>
- (void)addItemViewController;
#end
#interface DummyView : UIViewController
#property (nonatomic, weak) id <DummyViewDelegate> delegate;
#end
DummyView.m
#import "DummyView.h"
#interface DummyView ()
#end
#implementation DummyView
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)backButtonClicked:(id)sender {
[self.delegate addItemViewController];
}
ViewController.h
#import <UIKit/UIKit.h>
#import "DummyView.h"
#interface ViewController : UIViewController <DummyViewDelegate>
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
{
DummyView *acController;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
acController.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)addItemViewController{
}
Why not just make a tracking view controller available in the SDK. The app dev should then make all of their view controllers of this base type. You might need a couple of base classes (e.g. if you're also using TableViewControllers).
In SDK:
#interface TrackingViewController : UIViewController
#end
#implementation TrackingViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Make the call here
// ....
}
In app:
#import "SDK.h"
#interface MyViewController : TrackingViewController ...

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

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.

segue.identifier isEqualToString:#"segue1" is always null. IOS 6.1 storyboard

I can't figure out why my segue.identifier is always returning null. I set the segue identifiers in the storboard.
ViewController1
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(#"prepared %#",segue.identifier);
if([[segue identifier] isEqualToString:#"segue1"] ){
NSLog(#"segue is equal to 1");
}
}
ViewController2
- (IBAction)unwindFromSegue:(UIStoryboardSegue *)segue {
NSLog(#"unwinded %#",segue.identifier);
}
The log message is always null so if statement is never true. Any thoughts why i cant get the identifier?
EDIT:
I made a whole new project for testing. It's a single view application with ViewController and SecondViewController. In the storyboard I made a popover segue connection from a button in View Controller to SecondViewController and set the segue identifier to segue1.
On SecondViewController I made a back button and connected it to the unwindFromSegue in my ViewController. I also added prepareForSegue in the SecondViewController.
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#end
ViewController.m
#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.
}
- (IBAction)unwindFromSegue:(UIStoryboardSegue *)segue {
NSLog(#"unwinded %#",segue.identifier);
}
#end
SecondViewController.h
#import <UIKit/UIKit.h>
#interface SecondViewController : UIViewController
#end
SecondViewController.m
#import "SecondViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
- (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.
}
//was just testing unwindFromSegue here. Didn't mean to post here,
//but I will keep it in with an answer referring to it
//- (IBAction)unwindFromSegue:(UIStoryboardSegue *)segue{
//NSLog(#"unwind in second %#",segue.identifier);
//}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSLog(#"prepare %#",segue.identifier);
}
#end
Hope this helps. Thanks
When you set the segue in the storyboard you also have to assign it an identifier. In your case "segue1".
Why do you have an unwindFromSegue: method in SecondViewController? You're not unwinding to that controller, and it has the same name as the one in ViewController. That could be your problem. Take that one out and see if that fixes the problem.

Resources