随机网站按钮 [英] Random websites button
本文介绍了随机网站按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这个很简单我想但是我找不到适合我的问题的答案。
我想制作一个按钮,从我给他的列表中打开一个随机URL,比方说 - 谷歌,youtube和facebook就是这个例子。
这是我的代码行,现在只连接到谷歌......:
this one is pretty simple I guess but I just can't find the answer that would suit perfectly for my issue. I want to make a button that opens a random URL from a list I'll give him, let's say - google, youtube and facebook just for the example. This is my line of code that connects now only to google...:
- (IBAction)site:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://google.com"]];
}
有人可以告诉我添加到代码中的内容,以便随机选择这些其他网站也是?
Can someone please advise me what to add to the code so it will randomly pick these other websites as well?
推荐答案
像Popeye所说,你可以将URL存储到 NSArray
并随机选择其中一个:
Like Popeye said, you can store the URLs into a NSArray
and pick one of them randomly:
#include <stdlib.h>
- (IBAction)site:(id)sender {
NSArray *urls = @[
[NSURL URLWithString:@"http://www.google.com"],
[NSURL URLWithString:@"http://www.facebook.com"],
[NSURL URLWithString:@"http://www.twitter.com"]
];
int index = arc4random_uniform(urls.count);
NSURL *randomURL = urls[index];
if ([[UIApplication sharedApplication] canOpenURL:randomURL])
[[UIApplication sharedApplication] openURL:randomURL];
}
这篇关于随机网站按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文