如何在React Native中调用AlertIOS

时间:2022-09-04 10:06:19

I am a iOS developer, but I know a a little javascript. I am trying to use the AlertIOS, the document api is this

我是iOS开发人员,但我知道一点点javascript。我正在尝试使用AlertIOS,文档api就是这个

static alert(title: string, message?: string, buttons?: Array<{ text: ?string; onPress: ?Function; }>) 

I am confused with the parameters. I tried to write like this, but it says it's wrong.

我对参数感到困惑。我试着这样写,但它说这是错的。

AlertIOS('Username empty', 'Please type your username', buttons: {{text: 'Cancel', onPress: onPressCancel}});

Could anyone explain me how to call this function and the syntax?

任何人都可以解释我如何调用此函数和语法?

1 个解决方案

#1


4  

If you look at the documentation, it's says that there's an AlertIOS API with a static method called alert. That means you can call it like this:

如果你查看文档,就会说有一个AlertIOS API,它带有一个名为alert的静态方法。这意味着你可以像这样调用它:

AlertIOS.alert('Username empty', 'Please type your username', [{text: 'Cancel', onPress: onPressCancel}]);

Notice that you also don't need the "buttons:" prefix for the buttons array - that part of your call wasn't valid syntax anyway.

请注意,您还不需要按钮数组的“buttons:”前缀 - 无论如何,您的调用部分都不是有效的语法。

The method signature for alert is documented using Flow type annotations. Each argument is described like this:

使用Flow类型注释记录警报的方法签名。每个参数都描述如下:

  • name of argument: type of argument
  • 参数名称:参数类型

And if the name has a question mark, that argument is optional. So, in this case the arguments are:

如果名称有问号,那么该参数是可选的。因此,在这种情况下,参数是:

  • title, with a type of string
  • 标题,带有一种字符串
  • message, with a type of string (optional)
  • 消息,带有字符串类型(可选)
  • buttons, with a type of array (optional)
  • 按钮,带有一种数组(可选)

You'll also need to make sure you require the AlertIOS API, probably something like this:

您还需要确保需要AlertIOS API,可能是这样的:

var {
  AppRegistry,
  StyleSheet,
  View,
  AlertIOS
} = React;

Hope that helps.

希望有所帮助。

#1


4  

If you look at the documentation, it's says that there's an AlertIOS API with a static method called alert. That means you can call it like this:

如果你查看文档,就会说有一个AlertIOS API,它带有一个名为alert的静态方法。这意味着你可以像这样调用它:

AlertIOS.alert('Username empty', 'Please type your username', [{text: 'Cancel', onPress: onPressCancel}]);

Notice that you also don't need the "buttons:" prefix for the buttons array - that part of your call wasn't valid syntax anyway.

请注意,您还不需要按钮数组的“buttons:”前缀 - 无论如何,您的调用部分都不是有效的语法。

The method signature for alert is documented using Flow type annotations. Each argument is described like this:

使用Flow类型注释记录警报的方法签名。每个参数都描述如下:

  • name of argument: type of argument
  • 参数名称:参数类型

And if the name has a question mark, that argument is optional. So, in this case the arguments are:

如果名称有问号,那么该参数是可选的。因此,在这种情况下,参数是:

  • title, with a type of string
  • 标题,带有一种字符串
  • message, with a type of string (optional)
  • 消息,带有字符串类型(可选)
  • buttons, with a type of array (optional)
  • 按钮,带有一种数组(可选)

You'll also need to make sure you require the AlertIOS API, probably something like this:

您还需要确保需要AlertIOS API,可能是这样的:

var {
  AppRegistry,
  StyleSheet,
  View,
  AlertIOS
} = React;

Hope that helps.

希望有所帮助。