2012-04-11 4 views
1

Im는 스토리 보드와 함께 Xcode 4.3.2를 사용하므로 초보자이므로 코드 지식이 좋지 않습니다. 지금까지 주 뷰 컨트롤러가 데이터를 포함하는 내 tableview에 링크를 만들었습니다. 그러나 새로운 뷰 컨트롤러에 연결하는 방법을 모릅니다. 즉, 새로운 뷰 컨트롤러를 스토리 보드 페이지로 드래그하여 tableview에서 셀을 선택할 수있게하려는 다음 세부 정보가 포함 된 새 페이지를 엽니 다. 내 코드는 지금까지 자습서의 도움으로 수 있습니다 : -새 뷰 컨트롤러를 열려면 테이블 뷰에서 셀을 선택하는 방법?

Viewcontroller.h

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController <UITabBarDelegate, UITableViewDataSource, UISearchBarDelegate> 
    { 
    IBOutlet UITableView * tableView; 
    IBOutlet UISearchBar * searchBar; 

     NSArray * allItems; 
     NSMutableArray * displayItems; 
} 

@end 

에게 Viewcontroller.m

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    allItems = [[NSArray alloc] initWithObjects:@"K12", @"K14", @"K16", @"K18", @"K42", @"K43", @"K46", @"K53", @"K60", @"K68", @"K81", @"K83", nil]; 
    displayItems = [[NSMutableArray alloc] initWithArray:allItems]; 

} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1;  
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [displayItems count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; 
    } 
    cell.textLabel.text = [displayItems objectAtIndex:indexPath.row]; 
    return cell; 
} 

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { 
    if ([searchText length] == 0) { 
     [displayItems removeAllObjects]; 
     [displayItems addObjectsFromArray:allItems]; 
    } else { 
     //here 
     [displayItems removeAllObjects]; 
     for (NSString * string in allItems) { 
      NSRange r = [string rangeOfString:searchText options:NSCaseInsensitiveSearch]; 
      if (r.location != NSNotFound) { 
       [displayItems addObject:string]; 
     } 
    } 
} 

    [tableView reloadData]; 
} 

- (void)searchBarSearchButtonClicked:(UISearchBar *)aSearchBar { 
    [searchBar resignFirstResponder]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

@end 

난 그냥이 붙어 응용 프로그램의 나머지 부분을 작업을 편안하게 비트 및 검색 자습서에서는 다양한 코드를 살펴 보았습니다.하지만 지식이 제한되어 있기 때문에 추가 할 코드를 알려주는 것이 좋을뿐 아니라 더러운 가이드가 필요합니다.

감사합니다. 도움을 주시면 감사하겠습니다.

+0

선택한 textLabel의 내용에 따라 또는 선택한 행에 따라 다른 viewController를 표시하겠습니까? 검색 행을 기준으로 행 수가 달라지고 내용이 서로 다른 것처럼 보이므로 복잡한 점이 있습니다. 참고로 Storyboard에서 가능한지 확실하지 않습니다. 닙을 사용해야 할 가능성이 큽니다. – AMayes

+0

또한 다른 선택을 위해 실제로 다른 viewController를 사용해야하는지 또는 다른 viewController를 사용할 수 있는지 여부에 따라 달라집니다. – AMayes

+0

답변 해 주셔서 감사합니다. 예, 선택한 각 행은 새보기에서 다른 데이터를 포함하므로 "K12"를 선택하면 K12에 대한 정보가 자체보기/페이지를 표시합니다. 예를 들어 "ford vehicle"이라는 레이블을 붙인 경우 클릭하면 차량 세부 정보가 표시됩니다. Nibs에 대해서는 4.3.2 Xcode에서 nib 파일을 추가하는 방법을 잘 모릅니다. 스토리 보드에 새로운 뷰 컨트롤러를 드래그하여 연결하는 방법을 원했지만 펜촉을 사용해야한다면 도움을받을 수있었습니다. 시간 내 주셔서 감사드립니다. – Andy

답변

0

좋아, 나는 어떤 행이 선택되었는지에 관계없이 동일한 펜촉을 사용하여 정보를 표시한다고 가정합니다. 먼저 "보기"폴더를 마우스 오른쪽 버튼으로 클릭하거나 Ctrl 키를 누른 상태에서 "새 파일"을 선택하십시오. .xib를 사용하여 새 UIViewController 클래스를 만듭니다. 내 뷰 컨트롤러에는 자동차 이름에 대한 레이블, 자동차의 UIImageView (.jpg 부록과 함께 자동차와 같은 이름을 가진 이미지로 채워진 자동차의 UIImageView), 사전에 의해 채워지는 textView . 그래서 마찬가지로, 텍스트 뷰는 차에 대한 정보를 표시합니다, 나는 차의 이름으로 키가, 해당 정보로 사전을 사전로드 한 :

[dictionary setObject:@"This is a car." forKey:@"Ford Taurus"]; 

을 사전은 싱글, 그래서 모든 클래스에 액세스 할 수 있습니다 그것을 구현하는 방법은 https://stackoverflow.com/a/10093449/542400을 참조하십시오. 지금 내가 가진 것을 내 새의 ViewController 행이 선택되었을 때 합성 된 문자열 (나의 첫번째의 ViewController가 액세스 할 수), 내가 처음의 ViewController가 응답 할 것입니다 있는지 확인합니다으로, (의는 "NewVC"를 부르 자) :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    NewVC *new = [[NewVC alloc] init]; 
    //assuming you have no array from which you populated your table 
    new.myString = [[[myTable cellForRowAtIndexPath:indexPath] textLabel] text]; 

    //assuming you have a navigationController 
    [self.navigationController pushViewController:new animated:YES]; 
} 

-(void)viewWillAppear:(BOOL)animated{ 
    [myLabel setText:myString]; 
    NSString *string = [NSString stringWithFormat:@"%@.jpg", myString]; 
    UIImage *image = [UIImage imageNamed:string]; 
    [myImageView setImage:image]; 
    //see above for sharedDictionary info 
    [myTextView setText: [[MainDictionary sharedDictionary]getStringForKey:myString]]; 
    [super viewWillAppear:YES]; 
} 

이것은 매우 기본적인 구현입니다,하지만 난 그게 프로세스가 명확하게 희망 : 이제 내 NewVC의 -viewWillAppear 방법

, 나는 다음을 구현합니다. 사전 키 및 이미지 이름에 대한 간단한 명명 규칙을 지정하면 작업을 훨씬 쉽게 할 수 있습니다.

관련 문제