call void in IBAction - ios

I have the following:
-(void)turnBlue:(DetailVC *) controller {
self.Label.text = controller.selected.name;
self.selected = controller.selected;
[controller.navigationController popViewControllerAnimated:YES];
}
I'm trying to enter from my ibaction here:
- (IBAction)pressButton:(id)sender {
//filler code here
}
I'm trying
[self turnBlue:selected];
But am getting 'incompatible pointer types sending...'
I've tried controller, what details am I missing?

It should be something like:
// Allocate or get already created instance of `DetailVC`
DetailVC *controller = [[DetailVC alloc] initWithNibName..........];
// Pass instance to `turnBlue` function:
[self turnBlue:controller];
turnBlue can only accept a parameter of type DetailVC, while you are passing selected which seems not to be of type DetailVC so you are receiving incompetible pointer type error.
Hope it helps!

Related

Passing data with delegate but still nil why?

I made 2 viewcontrollers and implemented on tabbar controller. I passed some data from A vc to B vc with using delegate. When I checked the log it showed me correct value. But when I moved to B vc the value I passed was nil. (the value is for tableview.) Here is my code.
in A vc
-(void)passData {
NSMutableDictionary *infoDic = [[NSMutableDictionary alloc] init];
[infoDic setObject:url forKey:#"file_url"];
[downloadArr addObject:fileInfoDic];
Bvc getDataFromA:downloadArr];
[Bvc reloadTableView];
}
in B vc
-(void)getDataFromA:(NSMutableArray *) downloadArr{
self.downloadArr = [downloadArr mutableCopy];
NSLog(#"my download list%#", self.downloadArr); // This time was ok.
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
NSLog(#"Array status %#", self.downloadArr);//This time it showed me nil
[self.tableView reloadData];
}
To have the array printed inside viewWillAppear without nil
NSLog(#"Array status %#", self.downloadArr);//This time it showed me nil
you need to give it a value before you show bVC from aVC , whatever you use present/segue/push , also don't forget to declare it as strong , you need to do this
bvc = [[self.tabBarController viewControllers] objectAtIndex:1];
[bvc loadViewIfNeeded];
[bvc getDataFromA:downloadArr];
Please check the way you move to B-VC and whether the instance of (B-VC) you used in the A -VC is same to self in the B-VC “viewWillAppear” method.
It seems when u move to B-VC, A new instance has been created.
Or you can implement the 'setDownloadArr' method, log the value and show when the value become nil.

How to Access instance Variable of one class to another class in objective c?

I had tried accessing value of IVAR of one viewcontroller to another viewcontroller by making use of property , but i am getting null value.
please let me know how to get value displayd in my secondviewcontroller.
You are doing the right way to get value of property. But you need to make sure myString has been assigned a value in init method of ViewController.
Try in ViewController.m:
- (instancetype)init {
self = [super init];
if (self) {
self.myString = #"My String";
}
return self;
}
Beside of it, make sure that myMetod of SecondViewController is called. If myMetod is called, "Print value My String" will be printed.

access to property found with isMemberOfClass

How can I get access to a property of a ViewController when I have created an object of it like this:
for (UIViewController* vc in self.navigationController.viewControllers)
{
if ([vc isMemberOfClass:NSClassFromString(#"myViewController")])
{
// change property value on viewcontroller vc, for instance: vc.myText = #"hello" ??
}
}
thanks in advance!
You need to cast to let the compiler know the data type (or cheat and set it by an indirect method):
myViewController *mvc = (myViewController *)vc;
mvc.myText = #"hello";
Note also that class names should start with a capital first letter.
Just use simply like this:
(myViewController *)vc.myText = #"hello"
myText needs to be public variable also.

How to pass value to another view controller?

I am new to IOS Programming. I am sending an integer value to another view controller from segue like this:
SlideShowViewController *destViewController = segue.destinationViewController;
[destViewController setSelectedButton:tagIndex];
I am successfully accessing this value in my SlideShowViewController.m file:
#synthesize selectedButton;
NSLog(#"%i",selectedButton);
Now I want to access this same value which saved in selectedButton in my other View Controller. So I did this in my viewDidLoad of AnotherViewController:
SlideShowViewController *button= [[SlideShowViewController alloc] init];
NSLog(#"selected button is %i",button.selectedButton);
But I am not getting the value here.
While going to second view controller do same as you did for first
SecondViewController *destViewController2 = segue.destinationViewController;
[destViewController2 setSelectedButton:tagIndex];
In your SlideShowViewController.h create a property to hold data.
In your SlideShowViewController.m synthesize property and remove
SlideShowViewController *button= [[SlideShowViewController alloc] init];
this line from SlideShowViewController.m. In this line your are creating new instance of SlideShowViewController.
In the last code section, you're creating a new instance of the SlideShowViewController. This instance won't have the same variables as the first instance. In order to access the selectedButton field, you need to have a reference to your first SlideShowViewController instance to access its properties.
// Here you create a new instance. You need to hang onto the destViewController
// pointer if you want to access its selectedButton property later. This means
// you'll need to use a global variable, or otherwise expose it in some way for
// future reference.
SlideShowViewController *destViewController = segue.destinationViewController;
[destViewController setSelectedButton:tagIndex];
Later, possibly in a different method, you could do something like:
// Note, we're referring to destViewController using self. I'm assuming you've
// set up a property on the self object to refer to destViewController outside
// of the scope of the initial initialization.
int selectedButtonIndex = self.destViewController.selectedButton;

Passing a value from NSObject class back to UIViewController

I don't know why this is being so difficult but I can't get this to work. Here's my basic flow:
I have a UIViewController with a subview UIView which itself has a subview UIButton. Clicking the button instantiates a new instance of a NSObject called TwitterController, creates a NSURL for the twitter feed and then hands control over to TC to do the URLConnection and serialize the data returned.
Here's the relevant code in ViewController (Pruit_Igoe is me, feel free to follow though I don't post much : D) :
- (void) getTwitter {
//load new manager
twitterManager = [TwitterController new];
[twitterManager showTwitterFeed:vTwitterFeed:self];
NSURL* twitterFeedPath = [NSURL URLWithString: #"http://api.twitter.com/1/statuses/user_timeline.json?screen_name=Pruit_Igoe"];
[twitterManager getTwitterFeed:twitterFeedPath];
//toggle the twitter view
[self toggleView:vTwitterFeed];
[self toggleView:vContactCard];
}
showTwitterFeed dumps the objects in the view vTwitterFeed (button to close the view, images, etc.)
getTwitterFeed begins the NSURLConnection process
in TwitterController, I get the twitter feed and process it here:
- (void)connectionDidFinishLoading:(NSURLConnection *)theConnection {
//do something with the data!
NSError *e = nil;
//parse the json data
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: receivedData options: NSJSONReadingMutableContainers error: &e];
//dump it into an array
tweetArray = [[NSMutableArray alloc] init];
for(NSDictionary* thisTweetDict in jsonArray) {
NSString* tweet = [thisTweetDict objectForKey:#"text"];
[tweetArray addObject:tweet];
}
}
this all works fine, log tweetArray and all the text is there, log thisTweetDict and all the ton of data Twitter sends back is there. The problem is I want to pass tweetArray back to ViewController but I can't seem to figure out how to.
I've done the following:
Tried returning TweetArray from getTwitterFeed but it came back as null (my guess is the method returned the array before the connection had finished)
Tried to put it in UserDefaults but I keep getting null (same guess as above, but then I put it in connectionDidFinish and still null)
Tried to pass a reference to ViewController to TwitterController and then call a method in VC to pass the array to but in TwitterController I error out because it says my instance of VC doesn't recognize the selector. (It's there, I've triple checked).
I am sure this is simple and I am just being dense but could someone help me with this?
Edit: Here's how I tried to pass it back to VC:
I would pass VC to TC using this method (this is in VC)
[twitterManager showTwitterFeed:vTwitterFeed:self];
in VC.h I had a UIViewController* thisViewController
in VC.m in the showTwitterFeed:
- (void) showTwitterFeed : (UIView* ) theTwitterView : (UIViewController* ) theViewController {
thisViewController = theViewController;
//...other code to build view objects
then in
- (void)connectionDidFinishLoading:(NSURLConnection *)theConnection {
...
for(NSDictionary* thisTweetDict in jsonArray) {
NSString* tweet = [thisTweetDict objectForKey:#"text"];
[tweetArray addObject:tweet];
}
[thisViewController getTwitterFeed:tweetArray]; //<--this would error out saying selector not available
back in VC.h
- (void) getTwitterFeed : (NSArray* ) theTwitterFeed;
and in VC.m
- (void) getTwitterFeed : (NSArray* ) theTwitterFeed {
NSLog(#"%#", theTwitterFeed);
}
You can't return it from getTwitterFeed because connectionDidFinishLoading has not been called yet.
What you need to do is set up a protocol in your TwitterController and make your ViewController the delegate of the TwitterController.
Then when connectionDidFinishLoading occurs and you save the twitter information you can call the function back to your delegate (the ViewController).
Create a function called something like twitterDataReceived:(NSDictionary *)tweetDict in the TwitterController protocol and call it in the connectionDidFinishLoading function: [self.delegate twitterDataReceived:thisTweetDict]; or if you just want to send the text make it twitterTextReceived:(NSString *)theTweet and call [self.delegate twitterTextReceived:tweet]; or use an array like twitterArrayReceived:(NSArray *)tweetArray and [self.delegate twitterArrayReceived:tweetArray];, or whatever you want to send back.
If you are unfamiliar with setting up a protocol and a delegate there are many questions available which will help you out, like this one:objective-c protocol delegates
You get an unrecognized selector because thisViewController is of type UIViewController which does not have this method defined (Although its not seen from the code you posted I am absolutely sure this is the case as you would get a compilation error when assigning thisViewController = theViewController and theViewController type is UIViewController)
So change thisViewController type to your customized view controller and also change the signature of
(void) showTwitterFeed : (UIView* ) theTwitterView : (UIViewController* ) theViewController
to be :
(void) showTwitterFeed : (UIView* ) theTwitterView : (<Your custom controller type>* ) theViewController

Resources