2014-12-03 8 views
1

좌표 쌍을 역 지오 코딩하여 사용자의 도시를 찾고 있지만 도시를 Motion-Kit 레이아웃으로 옮기는 데 어려움이 있습니다. 도시를 배치하기에 가장 좋은 방법은 무엇입니까? API의 다른 정보도 레이아웃에 추가하므로 동일한 문제가 발생할 수 있습니다.Motion-Kit 레이아웃에서 비동기 적으로 획득 된 데이터 사용

class HomeLayout < MK::Layout 

    def initialize(data) 
    @city = data[:city] 
    super 
    end 

    def layout 
    add UILabel, :location 
    end 

    def location_style 
    text @city 
    color :white.uicolor 
    font UIFont.fontWithName("Avenir", size: 22) 
    size_to_fit 
    center ['50%', 80] 
    end 

end 

내가 HomeScreen이 방법에서 @city 얻을 :

def city 
    loc = CLLocation.alloc.initWithLatitude App::Persistence['latitude'], longitude: App::Persistence['longitude'] 
    geo = CLGeocoder.new 
    geo.reverseGeocodeLocation loc, completionHandler: lambda { |result, x| 
    return result[0].locality 
    } 
    # This currently returns a CLGeocoder object, but I want it to return the city as a String. 
end 

내가 그렇게처럼 AppDelegate에에 on_activate에서에서 App::Persistence['latitude']를 얻을 :

을이 같은 기본 MK 레이아웃이 있습니다

def on_activate 
    BW::Location.get_once do |result| 
    if result.is_a?(CLLocation) 
     App::Persistence['latitude'] = result.coordinate.latitude 
     App::Persistence['longitude'] = result.coordinate.longitude 
     open HomeScreen.new 
    else 
     LocationError.handle(result[:error]) 
    end 
    end 
end 

어떤 도움도 감사하겠습니다. ated. 미리 감사드립니다.

답변

2

레이아웃을 인스턴스화하는 방법을 알아야하지만 그게 없어도 데이터를 사용할 수있을 때 해제되는 fetching location 메시지 지원을 고려해야합니다.

워크 플로는 다음과 같이 보일 것입니다 :

  • 가이 위치 데이터를 제공하지 않고 레이아웃 을 만들 수 있습니다. 그것은
  • 당신이
  • 레이아웃에 위치를 제공 지금 많은처럼, 도시의 위치를 ​​페치 "위치에 대기"상태로 시작합니다, 그것은보기가

class HomeLayout < MK::Layout def location(value) # update view locations. # you might also provide an `animate: true/false` argument, # so that you can update the UI w/out animation if the # location is available at startup end end

변경에 애니메이션을 적용 할 수 있습니다

이 작업을 완료하면 시동시 도시 데이터를 제공하는 두 번째 단계를 수행 할 수 있습니다. 데이터를 사용할 수 있다면 위와 같이 이니셜 라이저에 데이터를 전달할 수 있어야하며 레이아웃은 "로드 중"상태를 우회해야합니다.

내가 컨트롤러를 "멱등 원 (idempotent)"으로 만들기 때문에이 방법을 권장하는 이유가 있습니다. 시동시 위치 데이터가 제공되는지 여부에 관계없이 두 가지 경우를 모두 처리 할 수 ​​있습니다.

또한 get_once 블록 완료를 기다리기 전에 open HomeScreen.new 수 있습니다.