我有两个视图控制器
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];
}
}
我想将值从searchBar.text = searchStr的SearchViewController ==>传递给ContentViewController ==>以在compare:@“ the”中传递值
我是iOS开发的新手。
最佳答案
在property
中创建一个公共@ ContentViewController
,以保存texbox中的NSString
。然后在推动视图控制器之前进行设置。您可能遇到的情况:
A)您正在使用storyboard
定义contentcontroller
和searchcontroller
。在这种情况下,您可以用鼠标Ctrl将鼠标从ContentController
拖动到storyboard
文件,并为其创建一个SearchController.h
(因此,您现在可以从“搜索”中访问IBoutlet
,您可以可以在用户输入文本时设置字符串值)
B)您正在手动按下ContentController
,然后在按下之前设置属性。
C)您正在通过ContentController
推送它。然后使用方法-storyboard
。在被推送之前会调用此方法,因此您有机会在那里设置被推送控制器的属性。