2

I’m an absolute beginner (started learning to code two weeks ago), so apologies in advance for the very basic question. I’m having trouble passing information from one method to another.

Background: I have an array of phrases. I want to get a random phrase from the array, display it in the UI, and also make this same phrase available in a Share Sheet. I can correctly retrieve and display the random phrase in the UI when the button is tapped (calling my “randomPhrase” method from another class). That works fine.

Problem: I am unable to pass the phrase “currentPhrase” to the Share Sheet method. How do I make this currentPhrase available for use in an outside method. I’m using ARC.

// Call the randomPhrase method and keep the output as a string called currentPhrase (this line works)
- (IBAction)phraseButtonTapped {
NSString *currentPhrase = [self.otherFile randomPhrase];


// Display the currentPhrase string in the UIlabel (this line works)
self.phraseLabel.text = currentPhrase;  


// Output to log for checking (this line works)
NSLog(@"current content is %@", currentPhrase);          
}


// Now for the Share Sheet. I could do this below, but text doesn’t match the UI phrase.
// NSString *shareText = [self.otherFile randomPhrase];
// NSArray *itemsToShare = @[shareText];


// So, here I’m trying to populate the current phrase into the share sheet. (this doesn’t work).
- (IBAction)showActivityView:(id)sender {      
NSArray *itemsToShare = @[_currentPhrase];


// Make the share sheet (this works from here onwards if I use a test hardcoded string)
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];


// Show the share sheet view (this works from here onwards if I use a test hardcoded string)
[self presentViewController:activityVC animated:YES completion:nil]; 
}

Note: I tried using “return currentPhrase;” in the first method, but I get “Void method ‘phraseButtonTapped’ should not return a value”.

johnsampson
  • 165
  • 1
  • 1
  • 6
  • phraseButtonTapped is set to `IBAction`, which is actually `void`. That's why you get the 'should not return a value' error. – revolver Jan 10 '14 at 07:44
  • You might wanna read this as well. http://stackoverflow.com/questions/683211/method-syntax-in-objective-c – Desdenova Jan 10 '14 at 07:51
  • Make your method like this -(NSString*)phraseButtonTapped and then return currentPhrase. Did you try like this?? – Hussain Shabbir Jan 10 '14 at 08:58

3 Answers3

0

if phraseButtonTapped is called before showActivityView: (or if it doesnt matter, you just want to use the text of phraseLabel, whatever it is, in showActivityView:) then you can just use self.phraseLabel.text inside showActivityView: and that will effectively pass that information to that method

so try going

NSArray *itemsToShare = @[self.phraseLabel.text];
Fonix
  • 11,447
  • 3
  • 45
  • 74
  • glad to hear, but as jrturton said it isnt exactly best practise, should have some kind of underlying data structure you can access instead to get these values from. but i think maybe for your purposes that could be a bit overboard if you are just playing around, so i would say this is fine – Fonix Jan 10 '14 at 13:03
  • also you should mark the answer as answered, and upvote any other useful answers if you can – Fonix Jan 10 '14 at 13:30
  • thanks fonix, I marked jrturton for best explanation answer, and note yours as the answer that worked correctly for my immediate question. Thankyou again. – johnsampson Jan 10 '14 at 23:32
0

The problem is one of scope. You tap a button to get a phrase, and store this value in a local variable. This means that you can only access this value within that method.

Others have suggested reading the value from the label's text but that isn't really best practice. If you're learning, you might as well learn properly.

Hopefully you've heard of Model-View-Controller, which is the standard pattern used for iOS apps. The app you have is a good example of this, except you are missing the model part.

The controller is your view controller object. The view is the label that is showing the phrase. The model should, in your case, be an NSString property on the view controller.

Instead of storing the phrase in a local variable, store it in the property. It will then be available for use in the sharing method.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • Thanks jrturton. Very clear (and kind) explanation. Really appreciate this. I learned the most just from your single answer. – johnsampson Jan 10 '14 at 12:05
  • Happy to help. If you feel this has answered your question you can accept it by clicking the check mark button under the score, this lets others know your question has been answered. – jrturton Jan 10 '14 at 12:17
-1

If your want to pass "SOME THING" to a method, try following syntax

//declaration
-(id) functionA:( NSArray*) myArray {

}

// function call
[self functionA : myArrayData];  // myArrayData is declared somewhere else
PH.W
  • 123
  • 9