Search code examples
rubymotion

How to create a textView in ruby motion?


Im just getting started in RubyMotion with a history in Rails. I have a simple app with two views. The first view is a tableView that lists an array of 'categories'. Each category has a detail view. I originally set up the detail view to also be a tableView, but am changing to UILabel since I only have a small paragraph of static text for each category. I looked in to using a one row table in the detail and changing the cells height with content, but decided since I will only ever need one cell...it probably was improper use of a table and better use of UILabel (which as far as I can tell is for static text...not just a 'label'). Opinions or ideas on this are very welcome.

category_view_controller.rb

So, in my existing category_view_controller I have the following method that pushes the details view controller when a row is selected from the list of categories in the category view controller. As, of now I am creating my categories with their attributes (label and description) statically in app_deligate.rb. This works fine in the sim.

   def tableView(tableView, didSelectRowAtIndexPath:indexPath)

     case indexPath.section
      when 0
       cat, label, desc = Categories.categories_list[indexPath.row]
       detailsVC = DetailViewController.alloc.initWithStyle(UITableViewStyleGrouped,        category:cat, label:label, description:desc)
       navigationController.pushViewController(detailsVC, animated:true)
     end

    tableView.deselectRowAtIndexPath(indexPath, animated:true)
  end

DetailViewController.rb.

This controller inherits from UITableViewController. The code below is already modified to get rid of the tableView...which is why there is no table (you can see where I have started changing to a UILabel). This loads fine too with a simple white label that includes the detail text I set in the add_deligate file.

class DetailViewController < UITableViewController

 def initWithStyle(style, category:cat, label:label, description:desc)
   initWithStyle(style)
   @category = cat
   @description = desc
   @label_text = label
   self
 end


 def viewDidLoad
   super
   self.view.backgroundColor = UIColor.redColor

   @label = UILabel.alloc.initWithFrame([[20, 50], [280, 80]]).tap do |label|
    label.translatesAutoresizingMaskIntoConstraints = false
    label.text = @description
    label.lineBreakMode = UILineBreakModeTailTruncation
    label.numberOfLines = 0
    label.sizeToFit
    label.textColor = UIColor.colorWithHue(0.0, saturation:0.0, brightness:0.40, alpha:1.0)
    self.view.addSubview(label)
  end
 end  
end

My confusion

I dont know how to go about removing the tableView and just using simple static text through UILabel. Or, maybe there is yet a better way.

I know I need to change this line in the categories_views_controller that calls the details_view_controller. This line is still referencing UITableViewStyleGrouped...and should't be since there is no table to style.

detailsVC = DetailViewController.alloc.initWithStyle(UITableViewStyleGrouped, category:cat, label:label, description:desc)

I tried simply removing 'initWithStyle...but, this breaks the app.

detailsVC = DetailViewController.alloc.init(category:cat, label:label, description:desc)

I also need the details_view_controller to NOT inherit from < UITableViewController. This seems as easy as just removing or changing the inheritance. I tried...but, this breaks the app. I suspect either of these might work once I correctly remove the 'UITableViewStyleGrouped' from the details call in the categories_view_controller.

  class DetailViewController

and

 class DetailViewController < UIViewController

Also, when I set a background color in the viewDidLoad method inside the details_view_controller it does not have any effect. Not sure why.

 self.view.backgroundColor = UIColor.redColor

So, to recap. root view is a tableView of static categories and each category has a detail view. The detail view is loaded when a category in the list is tapped. The details view is a simple view with a UILabel element displaying that categories detailed description.

I hoping someone can point me in the direction of learning motion and making this code work as intended. Thanks


Solution

  • It looks like you have a lot of background reading to do. Not a problem -- we were all there at one point. But the issues you're running into appear to be a general lack of familiarity with Cocoa Touch.

    I would recommend reading the official Apple guides, including the View Controller ones.

    https://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html

    If you'd rather just start coding, ProMotion is going to be your best friend.

    https://github.com/clearsightstudio/ProMotion

    ProMotion abstracts away a lot of the messy Objective-C and allows you to just open and close screens with simple commands.

    class AppDelegate < ProMotion::AppDelegateParent
      def on_load
        open ListScreen.new(nav_bar: true)
      end
    end
    
    class ListScreen < ProMotion::TableScreen
      def table_data
        [{
          title: "",
          cells: [{
            title: "Cell 1",
            action: :selected_cell,
            arguments: { cell_number: 1 }
          }, {
            title: "Cell 2",
            action: :selected_cell,
            arguments: { cell_number: 2 }
          }]
        }]
      end
    
      def selected_cell(args={})
        if args[:cell_number] == 1
          open DetailScreen
        elsif args[:cell_number] == 2
          open OtherScreen
        end
      end
    end
    
    class DetailScreen < ProMotion::Screen
      title "Detail"
    
      def will_appear
        # Set up your UILabel and whatnot here
      end
    end