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”.