2012-01-29 66 views
0

這是programmtically當播放按鈕被按下顯示的UIViewController

UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] 
           initWithBarButtonSystemItem:UIBarButtonSystemItemPlay 
           target:self 
           action:@selector(playaudio:)]; 
systemItem1.style = UIBarButtonItemStyleBordered; 

-(void) playaudio: (id) sender 
{ 
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"theme" 
                ofType:@"mp3"]; 
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath]; 
    audioPlayer = [[AVAudioPlayer alloc] 
        initWithContentsOfURL:fileURL error:nil]; 

    audioPlayer.currentTime = 0; 
    [audioPlayer play]; 
    [fileURL release]; 

    UIViewController* flipViewController = [[UIViewController alloc]init]; 
    [self.view addSubview:flipViewController.view]; 
} 

回答

1

如果你的UIViewController存儲在NIB文件,你可以用它來打電話的UIViewController的正確方法:

FlipViewController *flipViewController = [[FlipViewController alloc] initWithNibName:@"flipView" bundle:nil]; 

那麼你可以添加它的觀點使用:

[self.view addSubview:flipViewController.view]; 

或者將它顯示爲模態視圖(如您的名字ř的UIViewController)

flipViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
[self presentModalViewController:flipViewController animated:YES]; 

看一看UIViewController類參考: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

編輯:這裏是駁回通知使用模態的視圖的方式。

你在你的UIViewController(調用您flipViewController一)設置一個觀察者:

- (void)setObserver { 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(notificationReceived:) 
               name:@"DismissModalView" 
               object:nil]; 
} 

- (void)notificationReceived:(NSNotification *)notification { 
    if ([[notification name] isEqualToString:@"DismissModalView"]) { 
     [self dismissModalViewControllerAnimated:YES]; 
    } 
} 

現在叫setObserver在viewDidLoad中。

記住刪除您的觀察者的dealloc:

- (void)dealloc { 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
    // other objects  
    [super dealloc]; 
} 

現在,當你想回來在你的模式視圖調用是這樣的:

- (IBAction)dismissMe:(id)sender { 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"DismissModalView" object:self]; 
} 

最後這部分的帖子到達的通知給你的觀察者。當觀察者得到這個特定的通知電話[self dismissModalViewControllerAnimated:YES];和你的模態觀點被駁回。

這是文檔:http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html

+0

不使用協議和委託方法我可以用presentmodalviewcontroller顯示的UIViewController當播放按鈕被按下 – user1120133 2012-01-29 22:22:38

+0

代表團是推薦的方式,但你可以使用通知爲好。 – Beppe 2012-01-29 23:44:01

+0

通知你可以請告訴我如何當按下播放按鈕時可以使用通知來顯示UIViewController。 – user1120133 2012-01-30 00:31:43

相關問題