Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rubymotion:如何通过一个addSubview调用向一个视图添加两个子视图?_Ruby_Rubymotion - Fatal编程技术网

Rubymotion:如何通过一个addSubview调用向一个视图添加两个子视图?

Rubymotion:如何通过一个addSubview调用向一个视图添加两个子视图?,ruby,rubymotion,Ruby,Rubymotion,在这里,我创建了两个视图(条形图和图标),我想调用@window.addSubview来添加它们 class AppDelegate def application(application, didFinishLaunchingWithOptions:launchOptions) @window = UIWindow.alloc.initWithFrame UIScreen.mainScreen.bounds @window.makeKeyAndVisible ba

在这里,我创建了两个视图(条形图和图标),我想调用
@window.addSubview
来添加它们

class AppDelegate
  def application(application, didFinishLaunchingWithOptions:launchOptions)
    @window = UIWindow.alloc.initWithFrame UIScreen.mainScreen.bounds
    @window.makeKeyAndVisible

    bar = UIView.alloc.initWithFrame [[0, 0], [320, 100]]

    icon= UIImageView.alloc.initWithFrame([[100,0], [100,100]])

    @window.addSubview bar   # I have two calls to addSubview
    @window.addSubview icon

  true
  end
end
我想要这样的东西:

@window.addSubview bar, icon


我意识到这种差异是名义上的,但似乎应该有一种方法可以同时在多个视图上调用
addSubview

据我所知,没有一种方法可以实现您想要的功能。通常的ruby方式是:

[bar,icon].each do |view|
  @window.addSubview view
end  
甚至更短:

[bar, icon].each{|v| @window.addSubView(v) }

我想说的是,你只是在乞求在以后的某一天这样做会让你自己和其他人绊倒,但如果你必须这样做,你可以重新打开
UIView
,自己定义这个方法

class UIView
  def addSubviews *views
    views.flatten.each { |view| addSubview view }
  end
end
这应该允许

@window.addSubviews [view_a, view_b]

# OR

@window.addSubviews view_a, view_b
在这里尝试一下就是一个完整的例子

class AppDelegate
  def application(application, didFinishLaunchingWithOptions:launchOptions)

    @window = UIWindow.alloc.initWithFrame UIScreen.mainScreen.bounds
    @window.makeKeyAndVisible

    view_a = UIView.alloc.initWithFrame [[100, 100], [100, 100]]
    view_a.backgroundColor = UIColor.greenColor

    view_b = UIView.alloc.initWithFrame [[200, 200], [100, 100]]
    view_b.backgroundColor = UIColor.redColor

    @window.addSubviews [view_a, view_b]

    true
  end
end

class UIView
  def addSubviews *views
    views.flatten.each { |view| addSubview view }
  end
end

这是可行的,但它和两个单独的调用一样庞大。我希望有更优雅的东西。addSubview方法添加子视图,而不是添加子视图数组。如果您对此不满意,可以实现addSubView方法并使用它。@siuying是正确的,addSubView方法是Apple SDK方法,与RubyMotion无关。如果您想要一个采用数组的方法,您需要自己实现它。
class AppDelegate
  def application(application, didFinishLaunchingWithOptions:launchOptions)

    @window = UIWindow.alloc.initWithFrame UIScreen.mainScreen.bounds
    @window.makeKeyAndVisible

    view_a = UIView.alloc.initWithFrame [[100, 100], [100, 100]]
    view_a.backgroundColor = UIColor.greenColor

    view_b = UIView.alloc.initWithFrame [[200, 200], [100, 100]]
    view_b.backgroundColor = UIColor.redColor

    @window.addSubviews [view_a, view_b]

    true
  end
end

class UIView
  def addSubviews *views
    views.flatten.each { |view| addSubview view }
  end
end