如何在ios的scrollview中添加文本字段?

时间:2015-09-24 06:29:04

标签: ios objective-c uiscrollview uitextfield

我正在制作一个应用程序,其中我从图库中选择照片,我想在每个图片或视频上显示一个文本字段,以便我想描述该图片或视频。

这里是显示照片的代码,但没有在scrollview的每张图片上方显示文本字段。

-(void)launchController
{
    ELCImagePickerController *elcPicker = [[ELCImagePickerController alloc]initImagePicker];

    elcPicker.maximumImagesCount = 100;
    elcPicker.returnsOriginalImage = YES;
    elcPicker.returnsImage = YES;
    elcPicker.onOrder = YES;
    elcPicker.mediaTypes = @[(NSString *)kUTTypeImage,(NSString *)kUTTypeMovie];
    elcPicker.imagePickerDelegate = self;
    [self presentViewController:elcPicker animated:YES completion:Nil];
}
-(void)launchSpecialController
{
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc]init];
    self.specialLibrary = library;
    NSMutableArray *groups = [NSMutableArray array];
    [_specialLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group , BOOL *stop){
        if(group){
            [groups addObject:group];

        }else{
        [self displayPickerForGroup:[groups objectAtIndex:0]];
        }


    } failureBlock:^(NSError *error) {
        chosenImages = nil;
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:@"Album Error: %@ - %@", [error localizedDescription], [error localizedRecoverySuggestion]] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];

        NSLog(@"A problem occured %@", [error description]);
        // an error here mean
    }];

}
- (void)displayPickerForGroup:(ALAssetsGroup *)group
{
    ELCAssetTablePicker *tablePicker = [[ELCAssetTablePicker alloc] initWithStyle:UITableViewStylePlain];
    tablePicker.singleSelection = YES;
    tablePicker.immediateReturn = YES;

    ELCImagePickerController *elcPicker = [[ELCImagePickerController alloc] initWithRootViewController:tablePicker];
    elcPicker.maximumImagesCount = 1;
    elcPicker.imagePickerDelegate = self;
    elcPicker.returnsOriginalImage = YES; //Only return the fullScreenImage, not the fullResolutionImage
    elcPicker.returnsImage = YES; //Return UIimage if YES. If NO, only return asset location information
    elcPicker.onOrder = NO; //For single image selection, do not display and return order of selected images
    tablePicker.parent = elcPicker;


    tablePicker.assetGroup = group;
    [tablePicker.assetGroup setAssetsFilter:[ALAssetsFilter allAssets]];

    [self presentViewController:elcPicker animated:YES completion:nil];
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    return YES;

}else{
    return  toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;

}


}
#pragma mark ELCImageControllerDelegate Methods
-(void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
{
    [self dismissViewControllerAnimated:YES completion:nil];
    imageScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 150, self.view.frame.size.width, 450)];
    [self.view addSubview:imageScroll];
    UITextField  *textfield1 = [[UITextField alloc]initWithFrame:CGRectMake(10, 100, 100, 40)];
    textfield1.backgroundColor = [UIColor greenColor];
    NSMutableArray *textfieldArray = [NSMutableArray arrayWithCapacity:[info count]];
    [textfieldArray addObject:textfield1];
    textfield1.text= @"hello";
    [imageScroll addSubview:textfield1];
    for(UIView *v in [imageScroll subviews]){
        [v removeFromSuperview];


    }
    CGRect workingFrame = imageScroll.frame;
    workingFrame.origin.x = 0;

    NSMutableArray *images = [NSMutableArray arrayWithCapacity:[info count]];
    for (NSDictionary *dict in info) {
        if ([dict objectForKey:UIImagePickerControllerMediaType] == ALAssetTypePhoto){
            if ([dict objectForKey:UIImagePickerControllerOriginalImage]){
                UIImage* image=[dict objectForKey:UIImagePickerControllerOriginalImage];
                [images addObject:image];

                UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
                [imageview setContentMode:UIViewContentModeScaleAspectFit];
                imageview.frame = workingFrame;

                [imageScroll addSubview:imageview];

                workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
            } else {
                NSLog(@"UIImagePickerControllerReferenceURL = %@", dict);
            }
        } else if ([dict objectForKey:UIImagePickerControllerMediaType] == ALAssetTypeVideo){
            if ([dict objectForKey:UIImagePickerControllerOriginalImage]){
                UIImage* image=[dict objectForKey:UIImagePickerControllerOriginalImage];

                [images addObject:image];

                UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
                [imageview setContentMode:UIViewContentModeScaleAspectFit];
                imageview.frame = workingFrame;

               [imageScroll addSubview:imageview];
                ;

                workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
            } else {
                NSLog(@"UIImagePickerControllerReferenceURL = %@", dict);
            }
        } else {
            NSLog(@"Uknown asset type");
        }
    }

    chosenImages = images;

    [imageScroll setPagingEnabled:YES];
    [imageScroll setContentSize:CGSizeMake(workingFrame.origin.x, workingFrame.size.height)];
}

- (void)elcImagePickerControllerDidCancel:(ELCImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];



}
- (void)viewDidLoad {
    //chosenImages = [[NSArray alloc]init];


    [super viewDidLoad];



    [self.view setBackgroundColor:[UIColor whiteColor]];

//    textfield1 = [[UITextField alloc]initWithFrame:CGRectMake(10, 100, 100, 40)];
//    textfield1.backgroundColor = [UIColor greenColor];
//    textfieldArray = [NSMutableArray arrayWithCapacity:[info count]];
//    [textfieldArray addObject:textfield1];
//    textfield1.text= @"hello";
//    [imageScroll addSubview:textfield1];


    UIButton *uploadimage = [[UIButton alloc]initWithFrame:CGRectMake(10, 30, 55, 55)];
    uploadimage.backgroundColor = [UIColor blueColor];
    [uploadimage setTitle:@"multiple images" forState:UIControlStateNormal];
    [uploadimage addTarget:self action:@selector(launchSpecialController) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:uploadimage];


    UIButton *singleimage = [[UIButton alloc]initWithFrame:CGRectMake(90, 30, 55, 55)];
    singleimage.backgroundColor = [UIColor blueColor];
    [singleimage setTitle:@"uploadimage" forState:UIControlStateNormal];
    [singleimage addTarget:self action:@selector(launchController) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:singleimage];


        imagePicker = [[UIImagePickerController alloc]init];

2 个答案:

答案 0 :(得分:1)

我不明白你的代码行。问题应该存在。

你为什么要做下面的事情?

[imageScroll addSubview:textfield1];
for(UIView *v in [imageScroll subviews]){
    [v removeFromSuperview];
}

在第一行中,您将文本字段添加到滚动视图,然后使用for循环删除所有子视图。它也会从scrollview中删除您的文本字段。如果要向用户显示文本字段,则不应执行此操作。

请尝试评论for循环。希望它对你有用。

//编辑从这里开始。

我认为您应该使用UICollectionView而不是UIScrollView。在collectionview的每个单元格中,您可以显示图像和文本文件。这对你来说很容易,而且看起来会更好。

由于

答案 1 :(得分:0)

在其中使用NSMutableArray存储UITextField值,并在同一按钮上使用带有功能的图像数组调用它。

NSMutableArray *arr = [NSMutablearray....];
        for (UIView *subV in self.view.subviews){
             if([subV isKindOfClass:[UITextField class]])
    {
                //store it in a NSDictionary, so later can still know which 
                //textField your text belongs,
                 NSDictionary *tempDic = [NSDictionary dictionaryWithObjectAndKey:subV.txt 
                                   ,subV.tag,/*or subVw.placeholder*/,nil];
                [arr addObject:tempDic];

           }
        }