tags:

views:

285

answers:

2

hi every , i have 20 HTML files ok ?

i am going to shake the iphone , after shook , one of 20 html files shows up as random . i don't know random value on the objective C . can u help me ? here is my code :

#pragma mark -
- (void)accelerometer:(UIAccelerometer *)accelerometer 
        didAccelerate:(UIAcceleration *)acceleration {
    {
        if (acceleration.x > kAccelerationThreshold 
            || acceleration.y > kAccelerationThreshold
            || acceleration.z > kAccelerationThreshold) {
         // image hidden 
      shakeIcon.hidden = YES;

      //Random HTML view But here show only one . 
      NSString *path = [[NSBundle mainBundle] pathForResource:@"webViewContent" ofType:@"html"];
      NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];

      NSString *htmlString = [[NSString alloc] initWithData: [readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];

      [self.falView loadHTMLString:htmlString baseURL:nil];
      [htmlString release];


        }
    }
}

for example my html file names are : Myweb 1 ,Myweb 2 , 3,4,5,6,7,8,9 ............

+2  A: 

You can get a random number by using rand(). Take a look at generating random numbers in objective c

stimms
+1, Anything you can do in C you can do in Objective-C.
Carl Norum
I understand `arc4random()` is a better random number generator
mga
Oh sure, if you're building an encryption tool you want really good random numbers but the presented domain is so small as to not matter at all.
stimms
+1  A: 

I picked this code up from somewhere in SO or on the net, sorry to original author that I can't attribute source correctly. Didn't expect I'd be reposting it again. I didn't test it extensively to ensure it will in fact generate

#define MYRAND(from, to) ((int)from + arc4random() % (to-from+1))
...

EDIT //

if (acceleration.x > kAccelerationThreshold 
    || acceleration.y > kAccelerationThreshold
    || acceleration.z > kAccelerationThreshold) {
    // image hidden 
     shakeIcon.hidden = YES;

     //expect html files to be at top level of main bundle.
     NSString *bundlePath = [[NSBundle mainBundle] bundlePath];

     // choose one of your html files at random
     NSString *localPath = [[NSString stringWithFormat:@"%@/Myweb%d.html",
      bundlePath, MYRAND(1,20)];

     NSURL *fileURL = [NSURL fileURLWithPath:localPath];
     [self.falView loadRequest: [[NSURLRequest alloc] initWithURL:fileURL]];

}
wkw
Thank you so much dear wkw ... works great :)
Momeks
dear wkw .. i have one problem again ! i want iphone shake detect one time no Several times. it means when iPhone shook , one of the html files shows up , after that i don't want repeat shake operation again. see this movie : http://www.momeks.com/HafezDemo.mp4
Momeks
any answer ? :-??
Momeks