2014-07-10 30 views
-1

任何人都可以教我IBAction如何使用isEqualToStringNSArray對象?任何人都可以教我IBAction如何使用「isEqualToString」到NSArray對象?

我創建了一個IBAction來播放聲音,只要用戶觸摸按鈕。

我的第一頁是TableView和Array的聲音數據列表。

「DetailModal」是來自其他ViewController的NSArray對象。

我使用AudioToolbox和我所創建的聲音文件soundID

如何使用isEqualToString鏈接的NSArray對象?

例如:

我創建soundID名單如下:

NSURL *buttonURL1 = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"A" ofType:@"m4a"]]; 
AudioServicesCreateSystemSoundID((__bridge CFURLRef)buttonURL1, &soundID1); 

NSURL *buttonURL2 = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"I" ofType:@"m4a"]]; 
AudioServicesCreateSystemSoundID((__bridge CFURLRef)buttonURL2, &soundID2); 

Detail2ViewController.h

@property (strong, nonatomic) NSArray *DetailModal; 

如何使用 「isEqualToString」到IBAction中的NSArray對象?

- (IBAction)Sound { 
    int sound = _DetailModal; 

    if ([_TitleLabel.text isEqualToString:@"https://stackoverflow.com/a/"]) { 
     AudioServicesPlaySystemSound(soundID1); 
    } 

    if ([_TitleLabel.text isEqualToString:@"/i/"]) { 
     AudioServicesPlaySystemSound(soundID2); 
    } 
} 

我都試過,但未能...

+0

你想做什麼?觸摸listView中的聲音名稱並在detailView上播放它? –

+0

你的代碼沒有意義。 '_DetailModal'大概是一個NSArray,不能被分配給'int'。 –

+0

如果'_TitleLabel'確實是一個標籤,爲什麼文本會發生變化,而您已經知道您已將其更改爲?爲什麼你需要將它與任何東西進行比較? –

回答

1

這是的NSDictionary,而不是NSArray的一個完美的候選人。當你建立你的NSDictionary你建立這樣的:

[mutableDict setObject:yourNSURLA forKey:@"A"]; 
[mutableDict setObject:yourNSURLB forKey:@"B"]; 

現在是超級容易打了聲:

- (IBAction)onClick1:(id)sender { 
    NSString *title = [(UIButton *)sender currentTitle]; 
    NSURL * soundFileURL = [mutableDict objectForKey:title]; 
    AudioServicesCreateSystemSoundID((CFURLRef)soundFileURL, &_MySound); 
    AudioServicesPlaySystemSound(_MySound); 
} 
+0

這是一個很好的答案,但我建議你展示完整的IBAction方法,包括使用'sender'來檢索激活按鈕而不是'_TitleLabel' – Paulw11

+0

我的+1在哪裏是好答案:-p我加了更多細節感謝保持質量。 – Mika

0

你需要做兩件事情。首先,分配按鈕一個標題,然後閱讀它。

我的代碼類似於你想要做的,所以你應該能夠適應你的需求。在我創建按鈕的代碼段中,我給它一個標題和一個當它被按下時執行的方法。

[self.wordButton setTitle:@"w" forState:UIControlStateNormal]; 
[self.wordButton addTarget:self 
         action:@selector(wordButtonPressed:) 

然後,我讀了標題和做的東西。

- (IBAction)wordButtonPressed:(UIButton *)sender { 

    NSString *whichSnd = [NSString stringWithFormat:@"%@",sender.titleLabel.text]; 
    id <SayButtonsDelegate> SB_delegate = _delegate; 
    [SB_delegate sayButtonPressed:whichSnd]; 
} 

在我的情況下,我將titleLabel發送給委託人,並根據標題決定播放哪個聲音。 w表示播放單詞聲音,snd表示播放單詞中的聲音,alpha表示聲音中的字母。但我認爲你可以用if語句來處理它。

相關問題