0

我想知道是否searchcontroller可以将结果推送到detailviewcontrollerfrom a switch 语句。

这是我当前设置应用程序的方式。

。H

@interface MOVTableViewController : UITableViewController {

    NSMutableArray* filteredList;

    IBOutlet UISearchBar *search;
}

@property(strong, nonatomic) NSArray *lodgeList;
@property(strong, nonatomic) NSArray *cityname;



@end

.m

    #import "MOVTableViewController.h"
    #import "MOVDetailViewController.h"

    @interface MOVTableViewController ()

    @end

    @implementation MOVTableViewController

    @synthesize lodgeList, cityname;


    - (id)initWithStyle:(UITableViewStyle)style
    {
        self = [super initWithStyle:style];
        if (self) {
            // Custom initialization
        }
        return self;
    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];


        //Start of Lodge List

        lodgeList = [[NSArray alloc]initWithObjects:

                     //Abingdon
                     @"Abingdon Lodge No. 48",
                     @"York Lodge No. 12",

                     //Alberene
                     @"Alberene Lodge No. 277",

                     // Alexandria
                     @"A. Douglas Smith, Jr. No. 1949",
                     @"Alexandria-Washington Lodge No. 22",
                     @"Andrew Jackson Lodge No. 120",
                     @"Henry Knox Field Lodge No. 349",
                     @"John Blair Lodge No. 187",
                     @"Mount Vernon Lodge No. 219",......

    filteredList = [[NSMutableArray alloc] initWithArray:lodgeList];

    //end of list

}
    - (void)viewDidUnload
{

    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return [filteredList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }


    cell.detailTextLabel.text =[cityname objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = [filteredList objectAtIndex:indexPath.row];

    return cell;
}


- (void) searchTableView
{

    filteredList = nil;

    filteredList = [[NSMutableArray alloc] initWithArray:lodgeList];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like[c] %@",[NSString stringWithFormat:@"%@*",search.text]];
    [filteredList filterUsingPredicate:predicate];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [self searchTableView];


}

- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
    [self searchTableView];
    [search resignFirstResponder];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self searchTableView];
    [search resignFirstResponder];
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        [self performSegueWithIdentifier: @"lodgedetail" sender:self];
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{
    if ([segue.identifier isEqualToString:@"lodgedetail"]) {
        MOVDetailViewController *MDVC = segue.destinationViewController;

        NSIndexPath * path = [self.tableView indexPathForSelectedRow];
        NSString * thelodge = [filteredList objectAtIndex:path.row];

        MDVC.lodgeName = thelodge;
        MDVC = [segue destinationViewController];
        MDVC.lodgeNumber = path.row;


        NSIndexPath *indexPath = nil;

        if ([self.searchDisplayController isActive]) {
            indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
            MDVC.lodgeName = [filteredList objectAtIndex:indexPath.row];


        } else {
            indexPath = [self.tableView indexPathForSelectedRow];
            MDVC.lodgeName = [filteredList objectAtIndex:indexPath.row];


        }
    }


}



@end

.h(详细视图)

@interface MOVDetailViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextView *address;
@property (strong, nonatomic) IBOutlet UITextView *website;
@property (strong, nonatomic) IBOutlet UITextView *statedc;
@property (strong, nonatomic) IBOutlet UITextView *lodgehistory;
@property (strong, nonatomic) NSString * lodgeName;
@property int lodgeNumber;

@end

.m(详细视图)

#import "MOVDetailViewController.h"

@interface MOVDetailViewController ()

@end

@implementation MOVDetailViewController


@synthesize address, lodgeName, lodgeNumber, lodgehistory, statedc, website;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];



    self.title = lodgeName;

    switch (lodgeNumber) {
        case 0:
            //Abingdon Lodge No. 48
            address.text = @"325 W Main Street Abingdon, Virginia 24210";
            website.text = @"http://www.grandlodgeofvirginia.org/lodges/48/";
            statedc.text = @"Abingdon Lodge No. 48 holds it's stated communication on the 2nd Monday of the month at 7:30 PM. If there are any exceptions to the regular meeting day or time, they are posted on the Lodge website.";

            lodgehistory.text = @"According to the earliest records the Abingdon Lodge had its origin “At a meeting held at the house of James White in the town of Abingdon, on October 3, 1796.” The record states that a dispensation had been given by the Grand Master of Masons in Virginia and the following were present: William King, Master; Daniel Murphy, Past Master; Robert Johnson, Past Master; Charles Carson, Past Master, and William Preston Skillern. For more information on the Lodge History please see the Lodge website.";

            break;


            //York Lodge No. 12
        case 1:
            address.text =@"14411 Black Hollow Road Abingdon, Virginia 24210";

            website.text = @"http://www.grandlodgeofvirginia.org/lodges/12/";

            statedc.text = @"York Lodge No. 12 holds it's stated communication on the 4th Monday of the month at 7:30 PM. If there are any exceptions to the regular meeting day or time, they are posted on the Lodge website.";

            lodgehistory.text = @"York Lodge No. 12 was chartered on December 16, 1885. This Lodge was formed from a disturbance in Spring Creek Lodge No. 173, which was disbanded. The meetings were held in a schoolhouse at Maple Grove, Virginia on the second floor. The second floor caved in at some time and the meetings were then held on the first floor. In 1957 the Lodge voted to remodel the building for the cost of approximately five thousand dollars. In 1979 the December stated meeting was held in a new building that cost twenty thousand dollars. The fellowship hall is used by the Lodge, its members and the community for Church meetings, family functions, wedding dinners, arts and crafts and more. York Lodge is a part of its community and the community is a part of York Lodge.For more information on the Lodge History please see the Lodge website.";
            break;

            //Alberene Lodge No. 277
        case 2:
            address.text = @"2722 Plank Road Alberene, Virginia 22959";

            website.text = @"http://www.grandlodgeofvirginia.org/lodges/277";

            statedc.text = @"Alberene Lodge No. 277 holds it's stated communication on the 2nd Monday of the month at 7:30 PM. If there are any exceptions to the regular meeting day or time, they are posted on the Lodge website.";

            lodgehistory.text = @"Alberene Lodge No. 277 was chartered in 1900. The Lodge was destroyed by fire September 20, 1933, and the charter was reissued October 6, 1933. The 1 3/4 acres of land was purchased from Alberene Store Co. deeded August 25, 1937.  The first meeting place was over a store building about two miles from the present location.  Membership ranges from 40 to 60 members. The Lodge has remained small in number, but large in fellowship and real meaning in Masonry. It seems the Lodge will never go anywhere, by that it is meant never grow big or do anything great. By what all of the members have gained, the members will keep the Lodge in the best of Masonic health. For more information on the Lodge History please see the Lodge website.";
            break;


            //A. Douglas Smith, Jr. Lodge of Research No. 1949
        case 3:
            address.text = @"101 Callahan Drive Alexandria, Virginia 22301";

            website.text = @"http://www.grandlodgeofvirginia.org/lodges/1949/index.asp";

            statedc.text = @"A. Douglas Smith, Jr. Lodge of Research No. 1949 holds it's stated communication on the 5th Saturday of the month at 10:00 AM. If there are any exceptions to the regular meeting day or time, they are posted on the Lodge website.";

            lodgehistory.text = @"This Lodge of Research, the second in Virginia, was established in 1982. While there were several distinguished Masons in Northern Virginia who were instrumental in establishing the Lodge, the primary organizer was Most Worshipful Stewart W. Miner, Past Grand Master. He was most ably assisted in this effort by the District Deputy Grand Master of Masonic District No. 1A, Right Worshipful Werner H. Morlock, and Right Worshipful John H. King, Chairman of the Grand Lodge Committee on Lodge Development. Right Worshipful Christopher Dains and Worshipful William A. Myers, Past Master of Springfield Lodge No. 217 also provided assistance. Brother Morlock eventually served as Grand Master of Masons in Virginia in 1993-1994, but died in office before his term was completed. Worshipful Brother Myers became the first Worshipful Master of this Lodge. For more information on the Lodge History please see the Lodge website.";
            break;

我问的原因是因为在我执行搜索并单击其中一个结果后,detailviewcontroller它不会更新IBOulets UItextview地址、网站、静态或历史记录。它继续显示第一个小屋。但是,如果我不执行搜索,则 DVC 执行良好并相应地更新所有信息。

在此处输入图像描述

4

1 回答 1

1

prepareForSegue方法中,为什么要重新初始化视图控制器变量?尝试删除以下代码中提到的行:

MOVDetailViewController *MDVC = segue.destinationViewController;
NSIndexPath * path = [self.tableView indexPathForSelectedRow];
NSString * thelodge = [filteredList objectAtIndex:path.row];

MDVC.lodgeName = thelodge;
MDVC = [segue destinationViewController];  // REMOVE THIS LINE
MDVC.lodgeNumber = path.row;

请尝试这个更新的prepareForSegue方法:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"lodgedetail"]) {
        MOVDetailViewController *MDVC = segue.destinationViewController;

        NSIndexPath *indexPath = nil;
        if (self.searchDisplayController.isActive) {
            indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
            NSLog(@"filteredList = %@", filteredList);
            MDVC.lodgeName = [filteredList objectAtIndex:indexPath.row];

            // ******* Find row from the "lodgeList" array *******
            for(int i = 0; i < [lodgeList count]; i++) {
                if([MDVC.lodgeName isEqualToString: [lodgeList objectAtIndex: i]]) {
                    MDVC.lodgeNumber = i;
                    break;
                }
            }  

        } else {
            indexPath = [self.tableView indexPathForSelectedRow];
            MDVC.lodgeName = [lodgeList objectAtIndex:indexPath.row];
            MDVC.lodgeNumber = indexPath.row;
        }
        NSLog(@"indexPath = %@", indexPath);
    }
}
于 2012-11-19T01:07:20.037 回答