如何从其他视图控制器获取值并进行比较..?

时间:2015-07-07 10:27:28

标签: ios objective-c xcode6

我有两个视图控制器

  • SearchViewController.m
  • ContentViewController.m

在SearchViewController.m文件中

(void)searchBarSearchButtonClicked:(UISearchBar*)searchBar

 {
    [searchBar resignFirstResponder];

     NSString * searchStr = [searchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

     [searchBar resignFirstResponder];
     [self.searchDisplayController setActive:NO animated:YES];  
      **searchBar.text = searchStr**;  

     #ifndef SQL_KEYWORD_PARSE
    // Process the search string for keywords
     NSMutableSet* tokens = [[Tokenizer sharedTokenizer] tokenize:searchStr];

    // Create and perform a request for existing keyword records for these tokens
    NSError* err;
    NSFetchRequest* request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:@"Keyword"   inManagedObjectContext:self.managedObjectContext];
    request.predicate = [NSPredicate predicateWithFormat:@"keyword IN %@",tokens];
    NSArray* aKeywords = [self.managedObjectContext  executeFetchRequest:request error:&err];
   [request release];


   if (![aKeywords count])
{
    [[[[UIAlertView alloc] initWithTitle:nil
                                 message:@"No records match this search; it contains no indexed keywords"
                                delegate:self
                       cancelButtonTitle:@"Ok"
                       otherButtonTitles:nil] autorelease] show];
    return;
}
if (searchBar.selectedScopeButtonIndex && ([tokens count] != [aKeywords count]))
{
    [[[[UIAlertView alloc] initWithTitle:nil
                                 message:@"No records match this search; it contains one or more keywords with no matches"
                                delegate:self
                       cancelButtonTitle:@"Ok"
                       otherButtonTitles:nil] autorelease] show];
    return;
}

    // Create a filtered result set
   ((RuleSearchDataSource*) self.dataSource).exactPhrase = (searchBar.selectedScopeButtonIndex == 0);
   ((RuleSearchDataSource*) self.dataSource).useAll = (searchBar.selectedScopeButtonIndex == 1);
   ((RuleSearchDataSource*) self.dataSource).phrase = searchStr;
   ((RuleSearchDataSource*) self.dataSource).keywords = [NSSet setWithArray:aKeywords];
   #else
// Create a filtered result set
   ((RuleSearchDataSource*) self.dataSource).exactPhrase = (searchBar.selectedScopeButtonIndex == 0);
   ((RuleSearchDataSource*) self.dataSource).useAll =  (searchBar.selectedScopeButtonIndex == 1);
   ((RuleSearchDataSource*) self.dataSource).phrase = searchStr;
    #endif
    NSLog(@"String Search word :%@",searchStr);
}

ContentViewController.m文件

(void) reloadHighlights: (ContentViewDataSource *) dataSource
{
   // Clear any existing highlights.
    for (NSArray * section in dataSource.items)
    {
   if (section.count > 0)
    {
        TTTableStyledTextItem * item = section[0];
        ContentStyledText * text = (ContentStyledText *) item.text;
        [text removeHighlights];
    //          text.font = nil;
    }
}
AppDelegate_Shared * appDelegate = (AppDelegate_Shared *) [[UIApplication sharedApplication] delegate];
HighlightsController * highlightController = appDelegate.highlightsController;

NSMutableArray * highlights = highlightController.highlights;
DefaultStyleSheet * styleSheet = [DefaultStyleSheet globalStyleSheet];

if (highlights)
{
        if (self.tempHighlightPassageArray != nil)
    {
 for (NSNumber * passageId in self.tempMHighlightPassageArray)
        {
            int rowIndex = [(ContentViewDataSource *) self.dataSource rowNumberForPassage:[passageId integerValue]];

            TTTableStyledTextItem * item = (TTTableStyledTextItem *) [dataSource getSectionItem:rowIndex itemIndex: 0];

 if (item.text)
 {
    ContentStyledText * text = (ContentStyledText *) item.text;
    UIColor * highlightColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:1.0]; // 

     NSString* wordsList = @"";
     NSArray * paraArray = [text.rootNode.outerText componentsSeparatedByString:@" "];

 // for loop for passag word count
 for (NSUInteger iCount=0; iCount < [paraArray count ]; iCount++) 
 {
    if([paraArray[iCount] **compare:@"the"** options:NSCaseInsensitiveSearch]== NSOrderedSame)
    {
    if([wordsList isEqualToString:@""])
     {
       wordsList = [NSString stringWithFormat:@"%lu" , (unsigned long)iCount];
     }
     else{
         wordsList = [wordsList stringByAppendingString:@"|"];
         wordsList = [NSString stringWithFormat:@"%lu" , (unsigned long)iCount];
       }
  }
  if(![wordsList isEqualToString:@""]){
   NSArray* wordArray = [wordsList componentsSeparatedByString:@"|"];

   for (NSUInteger iCount=0; iCount < [wordArray count ]; iCount++)          {
    [text addHighlightFrom:[wordArray[iCount] integerValue] to:[ wordArray[iCount] integerValue] color:highlightColor stroke:highlightColor];
    //text.rootNode.outerHTML = text.rootNode.outerHTML;
    // return ;
             }
       }
 }
 }
        }
    }

    for (Highlight * highlight in highlights)
    {       
        if (highlight.sectionId == [self.section.id integerValue])
        {
            [self loadHighlight:highlight dataSource: dataSource styleSheet: styleSheet];
        }
    }
}

  if (mSelectionHighlight && mSelectionHighlight.highlightColor == kEraseColor)
  {
     // Erase color highlights don't get added to the list, but we still want to show a selection.
    [self loadHighlight:mSelectionHighlight dataSource: dataSource styleSheet: styleSheet];
  }

}

我想从SearchViewController传递值==&gt; searchBar.text = searchStr值到ContentViewController ==&gt;将值传递给比较:@“”

我是iOS开发的新手。

1 个答案:

答案 0 :(得分:1)

property中创建一个公开@ ContentViewController,以保留texbox中的NSString。然后在推动视图控制器之前设置它。您可能拥有的情况:

A)您使用storyboard来定义contentcontrollersearchcontroller。在这种情况下,您可以使用鼠标将ContentControllerstoryboard拖动到SearchController.h并为其创建IBoutlet(这样您就可以访问{来自Search one的{1}},您可以在用户输入文本时设置字符串值

B)您正在手动推动ContentController,然后在推送之前设置属性。

C)您正在通过ContentController推送它。然后使用方法 - storyboard。在推送之前调用此方法,以便您有机会在此处设置推送控制器的属性。