06-14-2009, 06:14 PM
#1 (permalink )
Registered Member
Join Date: May 2009
Posts: 44
Using random() in NSArray Help!
Hey, I have a series of NSDictionary commands. Each initiates a new page. What i'm trying to do is somehow have it so that when the user opens the application a random page will be shown each time. I have some code snips to show.
Datasource.m:
Code:
- (id)init
{
self = [super init];
if (self != nil)
{
dataPages = [[NSArray alloc] initWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:
@"Page 6", @"pageName",
@"Some text for page 5", @"pageText",
nil],
[NSDictionary dictionaryWithObjectsAndKeys:
@"Page 6", @"pageName",
@"Some text for page 5", @"pageText",
nil],
[NSDictionary dictionaryWithObjectsAndKeys:
@"Page 6", @"pageName",
@"Some text for page 5", @"pageText",
nil],
[NSDictionary dictionaryWithObjectsAndKeys:
@"Page 6", @"pageName",
@"Some text for page 5", @"pageText",
nil],
nil];
}
return self;
}
- (NSInteger)numDataPages
{
return [dataPages count];
}
- (NSDictionary *)dataForPage:(NSInteger)pageIndex
{
return [dataPages objectAtIndex:pageIndex];
}
@end
Code:
#import "PagingScrollViewController.h"
#import "PageViewController.h"
#import "DataSource.h"
@implementation PagingScrollViewController
- (void)applyNewIndex:(NSInteger)newIndex pageController:(PageViewController *)pageController
{
NSInteger pageCount = [[DataSource sharedDataSource] numDataPages];
BOOL outOfBounds = newIndex >= pageCount || newIndex < 0;
if (!outOfBounds)
{
CGRect pageFrame = pageController.view.frame;
pageFrame.origin.y = 0;
pageFrame.origin.x = scrollView.frame.size.width * newIndex;
pageController.view.frame = pageFrame;
}
else
{
CGRect pageFrame = pageController.view.frame;
pageFrame.origin.y = scrollView.frame.size.height;
pageController.view.frame = pageFrame;
}
pageController.pageIndex = newIndex;
}
- (void)viewDidLoad
{
currentPage = [[PageViewController alloc] initWithNibName:@"PageView" bundle:nil];
nextPage = [[PageViewController alloc] initWithNibName:@"PageView" bundle:nil];
[scrollView addSubview:currentPage.view];
[scrollView addSubview:nextPage.view];
NSInteger widthCount = [[DataSource sharedDataSource] numDataPages];
if (widthCount == 0)
{
widthCount = 1;
}
scrollView.contentSize =
CGSizeMake(
scrollView.frame.size.width * widthCount,
scrollView.frame.size.height);
scrollView.contentOffset = CGPointMake(0, 0);
pageControl.numberOfPages = [[DataSource sharedDataSource] numDataPages];
pageControl.currentPage = 0;
[self applyNewIndex:0 pageController:currentPage];
[self applyNewIndex:1 pageController:nextPage];
}
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
CGFloat pageWidth = scrollView.frame.size.width;
float fractionalPage = scrollView.contentOffset.x / pageWidth;
NSInteger lowerNumber = floor(fractionalPage);
NSInteger upperNumber = lowerNumber + 1;
if (lowerNumber == currentPage.pageIndex)
{
if (upperNumber != nextPage.pageIndex)
{
[self applyNewIndex:upperNumber pageController:nextPage];
}
}
else if (upperNumber == currentPage.pageIndex)
{
if (lowerNumber != nextPage.pageIndex)
{
[self applyNewIndex:lowerNumber pageController:nextPage];
}
}
else
{
if (lowerNumber == nextPage.pageIndex)
{
[self applyNewIndex:upperNumber pageController:currentPage];
}
else if (upperNumber == nextPage.pageIndex)
{
[self applyNewIndex:lowerNumber pageController:currentPage];
}
else
{
[self applyNewIndex:lowerNumber pageController:currentPage];
[self applyNewIndex:upperNumber pageController:nextPage];
}
}
[currentPage updateTextViews:YES];
[nextPage updateTextViews:YES];
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)newScrollView
{
CGFloat pageWidth = scrollView.frame.size.width;
float fractionalPage = scrollView.contentOffset.x / pageWidth;
NSInteger nearestNumber = lround(fractionalPage);
if (currentPage.pageIndex != nearestNumber)
{
PageViewController *swapController = currentPage;
currentPage = nextPage;
nextPage = swapController;
}
[currentPage updateTextViews:YES];
}
-(void)startApplication
{
codeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Email This MLIA"]];
codeButton.segmentedControlStyle = UISegmentedControlStyleBar;
codeButton.momentary = YES;
codeButton.tintColor = [UIColor darkGrayColor];
codeButton.center = CGPointMake(170,305);
[self.view addSubview:codeButton];
[codeButton addTarget:self action:@selector(simpleAction) forControlEvents:UIControlEventValueChanged];}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)newScrollView
{
[self scrollViewDidEndScrollingAnimation:newScrollView];
pageControl.currentPage = currentPage.pageIndex;
}
- (IBAction)changePage:(id)sender
{
NSInteger pageIndex = pageControl.currentPage;
// update the scroll view to the appropriate page
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * pageIndex;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];
}
- (void)dealloc
{
[currentPage release];
[nextPage release];
[super dealloc];
}
@end
If anyone could point me in the directin of where I should put the random() code to make it possible for a different page to be shown every time I open the application I would appreciate it very much
06-15-2009, 01:40 PM
#2 (permalink )
New Member
Join Date: Jun 2009
Posts: 1
try this....
- (void)applicationDidFinishLaunching
UIApplication *)application
{
...
srandom(time(NULL));
...
}
-------------------------------------------
PagingScrollViewController.m
- (void)viewDidLoad
{
....
NSUInteger numberOfTotalPages = ???;
NSUInteger randomPageNumber = random() % numberOfTotalPages;
pageControl.currentPage = randomPageNumber;
}
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 265
19 members and 246 guests
2WeeksToGo , @sandris , AdamL , ADY , Dani77 , diyora , F_Bryant , GHuebner , HDshot , headkaze , mer10 , Oral B , prchn4christ , Rudy , smithdale87 , Thompson22 , timle8n1 , Touchmint , vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,748
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris