iOS生成随机消息

时间:2014-05-14 03:31:35

标签: ios objective-c

你好,这是我正在研究的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

我想请一些帮助。

谢谢。

期望:

带框

Daily Vibes

- 附加了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.",

1 个答案:

答案 0 :(得分:1)

将您的消息放入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];
    }
}