2012-01-29 79 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

相关问题