Я абсолютный новичок (начал учиться программировать две недели назад), поэтому заранее извиняюсь за самый простой вопрос. У меня возникли проблемы с передачей информации от одного метода другому.Передача информации между методами в Objective-C
Фон: У меня есть множество фраз. Я хочу получить случайную фразу из массива, отобразить ее в пользовательском интерфейсе, а также сделать эту же фразу доступной в Share Sheet. Я могу правильно получить и отобразить случайную фразу в пользовательском интерфейсе при нажатии кнопки (вызов моего метода «randomPhrase» из другого класса). Это прекрасно работает.
Проблема: Я не могу передать фразу "currentPhrase" методу Share Sheet. Как сделать этот currentPhrase доступным для использования внешним методом. Я использую 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];
}
Примечание: Я попытался с помощью «возврата currentPhrase;» в первом способе, но я получаю «Пустота метод„phraseButtonTapped“не должен возвращать значение».
phraseButtonTapped установлен в 'IBAction', который фактически является' void'. Вот почему вы получаете ошибку «не возвращать значение». – revolver
Возможно, вы тоже захотите прочитать это. http://stackoverflow.com/questions/683211/method-syntax-in-objective-c – Desdenova
Сделайте свой метод следующим образом - (NSString *) phraseButtonTapped, а затем верните currentPhrase. Вы пробовали вот так? –