iOS生成随机消息

标签 ios objective-c

<分区>


想改进这个问题吗? 通过 editing this post 添加细节并澄清问题.

关闭 9 年前

您好,这是我正在处理的 iOS 项目。 我只是 iOS 应用程序编程的初学者。 我只是需要帮助,这样我才能改进 ><

到目前为止,这是我的应用程序。 iPhone Test

所以我的工作是在显示框“Daily Vibes”的提醒下进行的。 我需要随机包含 100 条消息。

我听说过弧随机函数。我只需要一些关于如何使用它的例子。

这是我的代码:

AppDelegate.m

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateActive) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Daily Vibes"
                                                        message:notification.alertBody
                                                       delegate:self cancelButtonTitle:@"OK"
                                                        otherButtonTitles:nil];
        [alert show];
    }

AddtoDoViewController.m

    implementation AddToDoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.itemText.delegate = self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)cancel:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)save:(id)sender {
    [self.itemText resignFirstResponder];

    // Get the current date
    NSDate *pickerDate = [self.datePicker date];

    // Schedule the notification
    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = pickerDate;
    localNotification.alertBody = self.itemText.text;
    localNotification.alertAction = @"Show me the item";
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

    // Request to reload table view data
    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self];

    // Dismiss the view controller
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.itemText resignFirstResponder];
    return NO;
}
@end

我需要一些帮助。

谢谢。

期望:

带盒子

每日共鸣

-随机附上100条消息-

肯定的例子:

NSarray *100affirmations = @[@"Every day in every way I am getting happier and happier.",
                            @"I am thankful to everybody who has touched my life and made it worth living. ",
                            @"Happiness is contagious. My happiness makes all these people happy, thus making it one big happy world.",
                            @"My happy disposition attracts happiness into my life and I only interact with happy people and have only happy experiences.",
                            @"I spread happiness to others and absorb happiness from others. I enjoy every moment of the day.",
                            @"Be happy is my motto and happiness is not a destination. It’s my way of life.",
                            @"I am living my life, feeling motivated and excited about the greatness I am creating, on a daily basis.",
                            @"I am going to make the best out of my life. I will appreciate all opportunities which are given to me and follow my way.",

最佳答案

将您的消息放在 NSMutableArray 中,我认为以下方法足以满足您的目的:

+ (void) randomOrderedMutableArray:(NSMutableArray *) array
{
    int count = [array count];
    for (int i = 0; i < count; ++i)
    {
        int nElements = count - i;
        int n = (arc4random() % nElements) + i;
        [array exchangeObjectAtIndex:i withObjectAtIndex:n];
    }
}

关于iOS生成随机消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23645171/

上一篇:ios - hitTest:withEvent: 两次调用 sendActionsForControlEvents

下一篇:ios - cocos2d v3 覆盖清理

相关文章:

ios - UILocalNotification 自定义 soundName 未播放

ios - 如何从数据库值创建 cupertinoactionsheet

ios - 设置UIBezierPath的lineWidth不起作用

objective-c - firemonkey + xcode,混合代码

ios - 如何以编程方式在 iOS 10 上的 Objective-C 中打开 WIFI 设置

ios - 创建图像的.Zip文件

ios - 有时 Xcode 8(自动布局)中有额外的间距(边距),为什么?

ios - 在 Objective C (iOS) 中生成一个介于 0.0001 和 0.002 之间的随机数?

iphone - Xcode 运行我删除的代码

c# - Objective-C 中是否有与 C# 的 yield 关键字相似的地方