Programmatical segue to new viewcontroller - ios

In an if/else function depending on the conditions that're met I want to show some string as result to the user. I'm trying to do this by changing to a new viewController programatically. I have created a method 'showresult' that will be taking one variable 'result' as argument and then changing viewController and showing the content of 'result' on there.
For changing the viewController I now have:
Defined in .h as:
Then I'm trying to call this getData method in an if/else function:
Here I get 'no visible #interface for 'NSString' declares the selector 'getData'
I assume the error lies in my definition of the method, I just can't see why
--UPDATED Part--
I have now reached a point where the following way of changin viewController works for me:
[self performSegueWithIdentifier:#"gotoresult" sender:self];
Now I just need the last part, which is to pass on a variable to the new viewController. I have found other threads about passing something on, but just didn't understand how to apply this.
One suggestion I found was this one, but prepareforSegue isn't recognized in my Xcode
-(void)prepareforSegue:(UIStoryboardSegue *)segue sender:(id)sender{
GTImageView * viewController = segue.destinationViewController;
viewController.someData = 99;
}
I just need to be able to show some string on to that new viewController

The compiler is not complaining about self because the identifier is unknown but because the way you declare your method is wrong. In Objective-C you define methods like this:
- (returnType)doSomethingWithTheFirstParameter:(param1Type)param1 secondParameter:(param2Type)param2 {
// body
}
The name of your method would be -doSomethingWithFirstParameter:secondParamter:.
In the same manner you can add as many parameters as you like. So in your case the correct method definition would be:
- (NSString *)showResult:(NSString *)result {
// body
}
As this is very basic stuff I suggest you first read an introduction to Objective-C or do a tutorial in order to get familiar with the syntax, e.g. Programming with Objective-C.
Question has been edited. Answer to your new question:
The compiler tells you what's wrong with that code: In your if-branch you call the message getData on the object result which seems to be of class NSString. But you added the getData method to your view controller class. So when you want to call that method from inside the view controller you need to use
[self getData:result];
instead.
Furthermore you should not return 0 for a method of return type NSString. If you do not want the method to return anything change the method definition to:
- (void)showResult:(NSString *)result {
// body
}
and remove the line
return 0;
or return some NSString object.
Note: Please do not include screenshots of your code but copy the code and paste the text to your post instead. Besides other advantages you give other users the option to copy your code and try it out.

Related

How and where are the methods defined within the ViewController.m file in my XCode project called?

Started learning Objective-C and XCode yesterday. Coming from a Rails background, I expect class methods like the ones defined in the ViewController.m file to be called at some point. I assume this is also the case in Objective-C, but I can't find where these methods are used.
To be more specific: I am working with a Collection View. I have the following method that defines the amount of sections in my view.
-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView*)collectionView{
return 1;
}
Firstly, I cannot find out where the method is called. How does numberOfSectionsInCollectionView get its UICollectionView argument?
Put another way - if this were Ruby and I defined the method like so:
Class ViewController
def numberOfSectionsInCollectionView(collectionView)
return 1
end
end
I would expect a view controller object (viewController = ViewController.new) to call the method somewhere (viewController.numberOfSectionsInCollectionView(argument)). I'm not seeing the objective-c equivalent here. Can anyone help me understand this?
As an aside, why would you pass in an argument to a method that's never used?

respondsToSelector to an id type object

In my class there is a method let say -aMethod: which takes an id type parameters and doing some operation with that object. Now I want to call a method let say -someMethod of that object. But the problem is object type is id and I can't cast it as I don't know which type of object will be come here. Below my code but it's showing error no know instant method for the selector someMethod. Can any body help to handle that type of situation.
I don't want to use block or delegate.
- (void)aMethod:(id)anObject
{
// other stuff
if([anObject respondsToSelector:#selector(someMethod)]) [anObject someMethod];
}
You simply have to show the compiler the method in any way. You need not to type the receiver that way. (But I would do that.) Personally I prefer to have a protocol that collects the methods.
#protocol MyDelegateProtocol
- (void)someMethod;
#end
// You do not have to use that protocol for typing.
// So you can use your code as you did.
Another way is to add a category to a class.
#interface AnyClassIncludingNSObject( MyDelegateAddition )
- (void)someMethod;
#end
Do not implement that category.
A third solution is to add the method to the class sending the message.
- (void)someMethod {}
It is always the same: The compiler has to see it one time.
If you don't know the exact class type that you will be sent you should create a protocol and have all classes that might be sent (and which implement the target method) confirm to that protocol. Now you can cast to (or set the parameter passed to) id < MyProtocol > and the compiler will be happy.
In addition to other answers here:
If for some reason you don't want to declare a protocol, you can do it like this:
if ([anObject respondsToSelector:#selector(someMethod)]) {
[anObject performSelector:#selector(someMethod)];
}
There is also performSelector:withObject: if you need to pass an argument, performSelectorOnMainThread and others.
As I understood, you don't want to do this the right way as other people suggested. So, in order to solve this, I would first remove the warning:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
... your code here ...
#pragma clang diagnostic pop
just move your code in between #pragmas.
Second, I would #import the class that has the someMethod declared, and just cast the the 'unknown' object to that class when calling the method.
Like: [(MySuperClassThatHasSomeMethodDeclared *)anObject someMethod];
And, yeah, don't worry, your someMethod will be called on the object you have passed.
So, here is your DIRTY way of solving the problem. Good Luck!
Create a subclass of NSObject which is a type of your requirement. In your case AnObject.
Now implement someMethod in this class and then use this class as an parameter instead of id.
So that you can able to call and implement someMethod as you expect.

Put Class results in a second viewController

I have an UIViewController in wich I imported a class:
#import "differenza.h"
and then I created an instance of that class when a button is pressed:
- (IBAction)ok:(id)sender {
differenza *classeDifferenza;
classeDifferenza = [[differenza alloc] init];
[classeDifferenza metodo];
}
As you can see, I also called a method.... now I don't have enough space in that UIViewController for the results of that method, so I need a new ViewController to show all the results... but I don't know how to recall results from that new ViewController, as I can not use the
classeDifferenza.variabile
way ....
This is something I did not really get already. In the past I used (thanks to your help: Setting up class instances in a multi view app (Objective C)) a "common class" where to store all data and all methods... but I don't think It's always the proper way... I feel like I am abusing that solution... or It's the right way to do what I described?
Thanks!
You can pass data with prepareForSegue check this topic
How to pass prepareForSegue: an object
You can send variable results like that

Calling method from within own class method

I have a method called LoginAttempt: with the following class header:
- (IBAction)LoginAttempt:(UIButton *)sender
and I'm trying to call this method in the same class' textFieldShouldReturn: method like this:
[self LoginAttempt:(SENDER TYPE HERE)];
The problem is, I can't figure out what to call where I put 'SENDER TYPE HERE', running Xcode 5, iOS 7.1 I believe, and Xcode has a suggestion: (UIButton *), like I'm supposed to enter a UIButton as the sender. Sorry if this is really amateur, but I'm really new to Objective-C and C, so I'm not really sure what it wants.
I have tried self.self, changing the cast on sender in LoginAttempt to id, and just taking away the (UIButton *).
How do I successfully call LoginAttempt:?
You can always just pass nil.
[self LoginAttempt:nil];
As the method is an IBAction, it's designed to be hooked up to a IB element and the sender argument will be a reference to the UI element that called the method. You can send anything or nothing though. Most likely, your implementation of the method doesn't even use the sender variable, so sending nil is more than fine.
And by the way, method names should begin with lowercase letters...

IOS super keyword causes errors

I'm updating the code in an app I didn't write while at the same time basically teaching myself objective C so am a beginner with this stuff.
I have a class called TableViewController and in it I have a method that is fired once a JSON feed successfully gets some data. This method is called:
- (void)JSONFetch:(MYJSONFetch *)fetch didDownloadCollection:(id)collection
{
//CODE HERE
}
I have a bunch of other classes that inherit from this class and in turn they call their own version of this method and add their own bits of functionality to the mix.
So for example I have a class called CategoryViewController that has this method defined in it:
- (void)JSONFetch:(MYJSONFetch *)fetch didDownloadCollection:(id)collection
{
[super didDownloadCollection:collection];
}
Notice the SUPER call there, ideally it should be able to access all the code in the parent's method and its own code as well.
In the .h file on the category controller I have this:
#import "MYTableViewController.h"
#interface MYCategoryViewController : MYTableViewController
{
//code here
}
So it should be able to 'see' the other code, but I get this error on the SUPER line:
Instance method '-didDownloadCollection' not found (return type defaults to 'id')
Which presumably means it actually can't see the parent method. It's not set as private as far as I can tell, the .h explicitly mentions the inheritance and other method calls happily bounce back and forth between the two.
So, something I've done has borken this good and I've no idea what. I've an inkling it must be in the MYJSONFetch code, but am not sure.
Anyone shed any light on this, hours spent trying to figure out why it can't see the parent method.
The method is
- (void)JSONFetch:(MYJSONFetch *)fetch didDownloadCollection:(id)collection
So you have to call
[super JSONFetch:fetch didDownloadCollection:collection];
Thats because the method is looks like this JSONFetch:didDownloadCollection: instead of this didDownloadCollection:.

Resources