2014-11-06 2 views
0

스토리 보드 모드를 사용하여 테이블을 호출하고 버튼을 호출하지 않습니다. 나는 그것을 슬쩍하게하는 코드를 가지고있다. 그러나 어떤 이유로 나는 버튼을 사이드 바라고 부르지 않는다. 나는 tableviewSideBar.swift 파일을 생성하여 기능을 제공하는 sideBartableViewController을 가지고 있습니다. sidebar.swift 파일에 버튼을 누를 때 sideBar을 열려면 추가 기능을 제공해야한다고 생각합니다. 내가 갖고있는 것은 스 와이프 동작이며 SideBar.swift 파일에 임베드되어 있습니다. 당신이 sideBarTableViewController 또는 SideBar의 코드가 필요하다면 어떤 도움을 내가SideBar decleration

당신은 뷰 계층에 sideBar.view을 추가해야
class ViewController: UIViewController, SideBarDelegate { 

var sideBar:SideBar = SideBar() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Menu Button 
    let button = UIButton.buttonWithType(UIButtonType.System) as UIButton 
    button.frame = CGRectMake(0, 17, 45, 43) 
    //button.backgroundColor = UIColor.greenColor() 
    //button.setTitle("Test Button", forState: UIControlState.Normal) 
    button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside) 
    self.view.addSubview(button) 
    var buttonMenu = UIImage(named: "menu-button.png") 
    var buttonMenuView = UIImageView(frame: CGRectMake(0, 17, 45, 43)) 
    buttonMenuView.image = buttonMenu 
    self.view.addSubview(buttonMenuView) 

    // Side bar action and text 
    sideBar = SideBar(sourceView: self.view, menuItems: ["Home", "Business Directory", "Classifieds", "Featured News", "Jobs", "Restaurants", "Sports"]) 
    sideBar.delegate = self 


} 

func buttonAction(sender:UIButton!) 
{ 
    if sideBar = SideBar.self{ 
     sideBarWillOpen() 

    }else{ sideBarWillClose() 
    } 



    } 
} 
+0

보기에 사이드 바가 추가 되었습니까? 뭔가 :'self.view.addSubview (sidebar.view)' –

+0

만약 내가 어디에 self.view.addSubview (sidebar.view) 넣어해야 해달라고? – Tom

답변

2

사이드 바가 UIViewController라고 잘못 가정했습니다. SideBar 클래스를 본 후에는 테이블 뷰 컨트롤러 표시/숨기기를 처리하는 NSObject를 볼 수 있습니다. 사이드 바가 열려 있는지 확인하고 그에 따라 표시/숨기기 만하면됩니다.

@IBAction func buttonAction(sender: AnyObject) { 
    if sideBar.isSideBarOpen { 
     sideBar.showSideBar(false) 
    } else { 
     sideBar.showSideBar(true) 
    } 
} 
+0

정말 고마워요! 나는 그것이 단지 어떻게하는지 알지 못하는 쉬운 전화라는 것을 알았다. 내가 여러 가지 방법으로이 작업을했는데 단순한 코드 일뿐입니다. 5 별 당신을 위해 내 좋은 선생 – Tom

+0

그냥 빨리 생각하면 사이드 바를 클릭하면 그것을 닫으라고 말할 것인가? – Tom

+0

열려있는 경우 사이드 바를 닫는 originView에 탭 제스처 인식기를 추가 할 수 있습니다. –

0

을 그것을 게시 할 수 있습니다! 감상 할 수있다.

다음은 맞춤 메뉴/사이드 바를 만드는 일반적인 방법입니다.

func showSideBar() { 
    // if sideBar is nil, then init it and set delegate 
    if sideBar == nil { 
     // init the sideBar 
     sideBar = SideBar(sourceView: self.view, menuItems: ["Home", "Business Directory", "Classifieds", "Featured News", "Jobs", "Restaurants", "Sports"]) 
     sideBar.delegate = self 
    } 
    // add it off the right side of the screen 
    sideBar.view.frame = CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height) // customize the width and height here 
    self.view.addSubview(sideBar.view) 
    // animate onto the screen 
    UIView.animateWithDuration(0.4, animations: {() 
     self.sideBar.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) 
    }) 
} 

func hideSideBar() { 
    if sideBar != nil { 
     // animate off the right side of the screen 
     UIView.animateWithDuration(0.4, animations: {() 
       self.sideBar.view.frame = CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height) 
     }) 
    } 
} 
+0

나는 코딩에 대해 아주 새로운데, 많은 것을 알지 못해서 나를 용서해 준다. 코드를 추가 할 때 오류가 발생합니다. 이 코드는 어디에 써야합니까? 내 ViewController, SideBar 또는 SideBarTableViewController에서? – Tom

+0

이것을 ViewController에 넣으십시오 –

+0

좋습니다! 그래서 그것을 내 ViewController에 넣고 sideBar == nil -> 형식의 인수 목록 (@Lvalue SideBar, NiLiteralConvertible) 및 sideBar.view.frame -> SideBar로 '=='을 호출 할 수 없다는 오류 메시지가 표시됩니다 'view'라는 멤버가 없습니다 – Tom