【问题标题】:How to add textfield in scrollview in ios?如何在ios的滚动视图中添加文本字段?
【发布时间】:2015-12-21 16:00:48
【问题描述】:

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

这是显示照片的代码,但不显示滚动视图中每张图片上方的文本字段。

-(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];

【问题讨论】:

  • 您是否遇到了特定问题?
  • 我在这里问过我想要什么,这就是问题......
  • 让我重新表述一下,您在创建UITextField 时遇到问题吗?还是将其添加到 scrollView?
  • 即使在编写代码之后,文本字段也没有显示在滚动视图上......

标签: ios objective-c uiscrollview uitextfield


【解决方案1】:

我不明白你的代码行。问题应该就在那里。

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

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

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

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

//编辑从这里开始。

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

谢谢

【讨论】:

    【解决方案2】:

    使用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];
    
               }
            }
    

    【讨论】:

      猜你喜欢
      • 2012-06-22
      • 2012-08-21
      • 1970-01-01
      • 2016-12-14
      • 1970-01-01
      • 1970-01-01
      • 2015-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多