iPhone scrollView使用ID动态添加元素 [英] iPhone scrollView add elements dynamically with id

查看:103
本文介绍了iPhone scrollView使用ID动态添加元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用很多不同的UI元素填充scrollView。
因此,我想我会写一种方法来记住滚动视图中的当前位置,然后将元素添加到当前位置的scrollView中。

I want to populate a scrollView with quite a few different UI elements. Therefore I thought I would write a method that remembers the current Position in the scrollView and just adds the element to the scrollView at the current Position.

类似:

- (void)addUIElement:(id)element withWidth:(CGFloat)width andHeight:(CGFloat)height andYGap:(CGFloat)YGap {

    element.frame = CGRectMake(currentScrollPos.x, (currentScrollPos.y + YGap), width, height);
    [scrolly addSubview:element];

    //And then set the current scroll position here
}

不幸的是,当我尝试访问element.frame = ...时,我收到的不是结构或联合的成员请求。当我尝试执行[元素框架] = ...左值作为赋值左操作数时。

Unfortunately when I try to do access element.frame = ..., I get request for member in something not a structure or union. When I try to do [element frame] = ... Lvalue required as left operand of assignment.

现在,首先我不确定什么是最好的方法动态地将对象添加到滚动视图。也许任何人都有更好或更轻松的方法。

Now, first of all I am not sure what's the best way to dynamically add objects to a scrollview. Maybe anyone has a better or easier approach.

那么,我不明白为什么上面的方法不起作用?!我是否需要将元素转换为实际的课程?我以为我不必这样做...而且我的方法也不再那么有意义了。或至少需要更多步骤...

Then on the other hand, I don't get why the above does not work?! Would I have to cast my element to the actual class? I thought I would not have to do so... Also then my method would not make that much sense anymore. Or at least would require some more steps...

推荐答案

我认为这应该可以工作:

This should work I think:

 [element setFrame:...];

但是,如果您在方法中使用不同的UI元素,则可以将元素参数设为UIView *而不是ID?这样,您的代码将适用于所有UIView子类(我想这是您实际上需要的)

However if you work with different UI elements in your method may be you can make your elements parameter UIView* instead of id? This way your code will work for all UIView subclasses (which is what you actually need I suppose)

这篇关于iPhone scrollView使用ID动态添加元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆