随机文本生成器 - iOS

时间:2012-12-09 18:00:23

标签: objective-c ios xcode random label

我想制作一个随机的文本生成器,我知道如何做一个常规的,但在这种情况下,我无法让它工作。所以我拍摄的代码或者选择了一张图片,然后将它显示在不同的视图控制器上,并且我希望每次拍摄照片时将最后一个视图控制器上的标签设置为随机句子。我不知道我是否正确解释了自己。 这是我的代码:

//ViewController.m   

     -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
        if([segue.identifier  isEqualToString:@"CameraSegue"] || [segue.identifier isEqualToString:@"LibrarySegue"])
        {

            UIImagePickerController *controller = [segue destinationViewController];
            controller.sourceType = [segue.identifier isEqualToString:@"LibrarySegue"] ?  UIImagePickerControllerSourceTypePhotoLibrary : UIImagePickerControllerSourceTypeCamera;
            controller.delegate = self;


        }
        else if([segue.identifier isEqualToString:@"ShowImageViewController"]){

            UIImage *image = (UIImage*)sender;
            ShowImageViewController *viewController = segue.destinationViewController;
            viewController.pickedImage = image;

            UILabel *label = (UILabel *) sender;
            ShowImageViewController *vc = segue.destinationViewController;
            vc.cap = label;
           //I tried to set the label here
            label.text = @"Hello";

        }
    }

    -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
        [self dismissViewControllerAnimated:YES completion:nil];
    }

    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        [self dismissViewControllerAnimated:YES completion:^{
            picker.delegate = nil;
            [self performSegueWithIdentifier:@"ShowImageViewController" sender:image];
        }];
    }

//SeconViewController.h
@property(nonatomic, strong) UIImage *pickedImage;
@property (weak, nonatomic) IBOutlet UIImageView *pickedImageView;
@property(nonatomic, retain) IBOutlet UILabel *cap;

//SecondViewController.m

-(void)viewWillAppear:(BOOL)animated{
    self.pickedImageView.image = self.pickedImage;
}

2 个答案:

答案 0 :(得分:0)

你可以这样做。它是一个c函数,但你可以在objective-c中使用它,或者转换它。

 void generateRandomString() {

      int numRandChars = 15;
      for ( int i = 0; i < numRandChars; i++ ) {

          int intChar = arc4random()%126;

          // Limit to above 33+
          while ( intChar < 33 ) { intChar = arc4random()%126; }

          // Use ascii table to convert
          char aChar = (char)toascii(intChar);

          // Print or do whatever you want with the string
          printf("\n%d = %c",intChar, aChar);
      }
 }

此外,如果您在设置标签时仍然遇到问题,则需要确定“发件人”实际上是什么,即确保它不是nil并且 实际上是UILabel如你所料。

希望有所帮助。

答案 1 :(得分:0)

在swift中,您可以使用它来获取辅音和元音,只需将其放在循环上并创建您的单词,希望它有所帮助

func getConsonant() {
    var consonant:NSString = "QWRTYPSDFGHJKLZXCVBNM"
    var selected = Character(UnicodeScalar(consonant.characterAtIndex(Int(arc4random_uniform(20)))))
    println("This is a consonant \(selected)")
}

func getVowel() {
    var vowel:NSString = "AEIOU"
    var selected = Character(UnicodeScalar(vowel.characterAtIndex(Int(arc4random_uniform(4)))))
    println("This is a vowel \(selected)")
}