Ios 如何通过视图控制器之间的按钮操作传递参数?

Ios 如何通过视图控制器之间的按钮操作传递参数?,ios,uiviewcontroller,Ios,Uiviewcontroller,刚接触objective-C,正在从事媒体播放器项目。我目前有2个视图控制器: MenuViewController playervewcontroller 在MenuViewController中,我有一个按钮操作,用于创建playervewcontroller的实例: - (IBAction)showPlayer:(id)sender { PlayerViewController * vc = [[PlayerViewController alloc] initWithNibNa

刚接触objective-C,正在从事媒体播放器项目。我目前有2个视图控制器:

  • MenuViewController
  • playervewcontroller
MenuViewController
中,我有一个按钮操作,用于创建
playervewcontroller
的实例:

- (IBAction)showPlayer:(id)sender
{
    PlayerViewController * vc = [[PlayerViewController alloc] initWithNibName:@"PlayerViewController" bundle:nil];


    [self presentModalViewController:vc animated:YES];

}
当按下按钮并且加载了
ViewPlayerController
时,它会自动启动视频播放。我想做的是在
MenuViewController
上创建一些按钮,并通过每个按钮传递参数(例如:“video1”、“video2”、“video3”等),然后能够在我的
playervontroller
中使用if语句,以便它检查参数并根据按下的按钮播放视频。是否可以将参数附加到vc上

我想做的是在MenuViewController上创建一些按钮,并通过每个按钮传递参数

只需在创建
playervewcontroller
之后和呈现它之前设置它的一些属性:

- (IBAction)showPlayer:(id)sender
{
    PlayerViewController * vc = [[PlayerViewController alloc] initWithNibName:@"PlayerViewController" bundle:nil];

    vc.someProperty = @"video1";

    [self presentModalViewController:vc animated:YES];
}

在演示之前,您可以根据需要配置
vc
,包括调用各种方法、为其提供所需的数据等。

在PlayerViewController中有一个名为videoIdentifier的属性,并将其设置为MenuViewController按钮下的操作方法,如

- (IBAction)showPlayer:(id)sender
{
PlayerViewController * vc = [[PlayerViewController alloc] initWithNibName:@"PlayerViewController" bundle:nil];
vc.videoIdentifier = sender.title

[self presentModalViewController:vc animated:YES];
}


在您的PlayerViewController中,比较switch语句if-else下的videoIdentifier属性以播放所需的视频。

根据您的要求设置按钮标题,如“video1”、“video2”、“video3”等。