<分区>
标签 ios objective-c
您好,这是我正在处理的 iOS 项目。 我只是 iOS 应用程序编程的初学者。 我只是需要帮助,这样我才能改进 ><
到目前为止,这是我的应用程序。
所以我的工作是在显示框“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 - UILocalNotification 自定义 soundName 未播放
ios - 如何从数据库值创建 cupertinoactionsheet
ios - 设置UIBezierPath的lineWidth不起作用
objective-c - firemonkey + xcode,混合代码
ios - 如何以编程方式在 iOS 10 上的 Objective-C 中打开 WIFI 设置
ios - 有时 Xcode 8(自动布局)中有额外的间距(边距),为什么?