简体   繁体   中英

Passing information between methods in Objective-C

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

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];

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM