Iphone 如何使用NSNotificationCenter将通知从一个UIButton发送到另一个UIButton

Iphone 如何使用NSNotificationCenter将通知从一个UIButton发送到另一个UIButton,iphone,Iphone,单击“回放”按钮时,此UIButton回放如何向UIButton play发送通知以执行playpauseaction方法 -(void)rewind:(id)sender{ [timer invalidate]; audioPlayer.currentTime = 0; MainViewController *viewController = [[MainViewController alloc] init]; viewController.view.frame = CGRectMake(0

单击“回放”按钮时,此UIButton回放如何向UIButton play发送通知以执行playpauseaction方法

-(void)rewind:(id)sender{
[timer invalidate]; 
audioPlayer.currentTime = 0;
MainViewController *viewController = [[MainViewController alloc] init];
viewController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:viewController.view];
[self.view addSubview:toolbar];
[viewController release];
} 

 -(void)playpauseAction:(id)sender {    
if([audioPlayer isPlaying])
{
    [sender setImage:[UIImage imageNamed:@"Play Icon.png"] forState:UIControlStateSelected];
    [audioPlayer pause];
    [self pauseTimer];
    [self pauseLayer:self.view.layer];
}
else
{
    [sender setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
    [audioPlayer play];
    [self resumeTimer];
    [self resumeLayer:self.view.layer];

    if(isFirstTime == YES)
    {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
                                                      target:self
                                                    selector:@selector(displayviewsAction:)
                                                    userInfo:nil
                                                     repeats:NO];
        isFirstTime = NO;
    }
    }
    }
单击“执行回放方法”时的“回放”按钮,以及向“播放”按钮发送通知以执行“播放暂停反应”方法

谢谢你的帮助

编辑 宣布

    @property (nonatomic, retain) UIButton *playButton;
    @synthesize playButton = _playButton; 
   [self playpauseAction:_playButton];

当点击回放按钮时,它会执行播放暂停动作方法,但不会按照播放暂停动作方法切换播放按钮以暂停。为什么会这样呢

如果你有一个播放暂停按钮的参考(你想让这类事情变得更容易),你可以用你的
倒带
方法写这一行

[self playpauseAction:playpauseButton];

你根本不需要使用通知中心

如果你有一个播放暂停按钮的参考(你想让这类事情变得更容易),你可以用你的
倒带
方法写这一行

[self playpauseAction:playpauseButton];

你根本不需要使用通知中心

在你的视图中,你写了这个

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playpauseAction) name:@"ButtonPressed" object:nil];
按下按钮时,添加此代码

[[NSNotificationCenter defaultCenter] postNotificationName:@"ButtonPressed" object:nil];
它应该叫你的playpauseAction

别忘了在你的书信里写

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ButtonPressed" object:nil];
否则,代码将崩溃,因为即使类的实例不再存在,该方法仍将尝试调用

希望这有帮助


乔纳森

在你看来,这是load写的

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playpauseAction) name:@"ButtonPressed" object:nil];
按下按钮时,添加此代码

[[NSNotificationCenter defaultCenter] postNotificationName:@"ButtonPressed" object:nil];
它应该叫你的playpauseAction

别忘了在你的书信里写

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ButtonPressed" object:nil];
否则,代码将崩溃,因为即使类的实例不再存在,该方法仍将尝试调用

希望这有帮助



Jonathan

所以基本上在倒带方法中,我应该添加这个[[NSNotificationCenter defaultCenter]postNotificationName:@“ButtonPressed”对象:nil];要发送通知播放按钮以执行播放PauseAction方法,请在添加的dealloc方法中单击[NSNotificationCenter defaultCenter]removeObserver:self name:kReachabilityChangedNotification对象:nil];并且得到了一条红色消息,使用未声明的标识符KreachabilityChangedNotification哇,抱歉,编辑了代码-应该马上!对于您的第一条评论,是的,您是正确的。现在,在倒带方法中添加notificationstatement后,当单击倒带按钮时,应用程序崩溃,消息是[NSConcreteNotification setImage:forState::]:未识别的选择器发送到实例0x155e70',因此基本上在倒带方法中,我应该添加这个[[NSNotificationCenter defaultCenter]postNotificationName:@“ButtonPressed”对象:nil];要发送通知播放按钮以执行playpauseaction方法,请在添加的DealLocal方法中正确执行[[NSNotificationCenter defaultCenter]removeObserver:自身名称:kReachabilityChangedNotification对象:nil];并收到一条红色消息,表示使用了未声明的标识符kReachabilityChangedNotificationWhoops,抱歉,编辑了代码-应该现在就开始!对于您的第一条评论,是的,您是正确的。现在在rewind方法中添加notificationstatement后,当单击rewind按钮时,应用程序崩溃,消息是这样的吗[NSConcreteNotification setImage:forState:::未识别的选择器发送到实例0x155e70'是的,您的权利,也许他们想从另一个类调用它(我不知道他们为什么要这样做),但我想这就是他们要求的…你的答案要好得多…;)如何获取播放暂停按钮的引用创建一个IBOutlet属性,并在interface builder中连接它,假设你当然在interface builder中创建了按钮,你已经创建了属性,但没有将其设置为任何值。当你创建按钮时(
[[UIButton alloc]init….]
)您需要将其分配给playButton属性,而不是
UIButton*playButton=[UIButton…]
尝试
self.playButton=[UIButton…]
是的,您的权利,也许他们可能想从另一个类调用它(我不知道他们为什么要这么做),但我想这就是他们要求的…你的答案要好得多…;)如何获取播放暂停按钮的引用创建一个IBOutlet属性,并在interface builder中连接它,假设你当然在interface builder中创建了按钮,你已经创建了属性,但没有将其设置为任何值。当你创建按钮时(
[[UIButton alloc]init….]
)您需要将其分配给playButton属性,而不是
UIButton*playButton=[UIButton…]
try
self.playButton=[UIButton…]