segue from searchResultsTableView - xcode4.5

I currently have a searchdisplaycontroller set up and working. The original table view has a segue set up for the title and image to be passed to a detail view. I would like this segue to happen from the search display controller but this is not happening? I know this is possible but can't seem to get it to work? My code is:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.detailViewController setDetailItem:[[contentsList objectAtIndex:indexPath.section]
objectAtIndex: indexPath.row]];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"detailview"]) {
self.detailViewController=segue.destinationViewController;
}
}
#end
and in the detail view to set it all up:
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
self.navigationItem.title = [self.detailItem objectForKey:#"name"];
[self.detailImage setImage:[UIImage imageNamed:self.detailItem[#"image"]]];
}
}
I would be very grateful for any help in solving this issue :)

You can Segue from a Search Display Controller by calling performSegueWithIdentifier:sender: from tableView:didSelectRowAtIndexPath: like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Get the UITableViewCell that was selected
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
// Then this will call prepareForSegue:sender: with selectedCell as sender
[self performSegueWithIdentifier:#"detailview" sender:selectedCell];
}
Then on prepareForSegue:sender: you set your destinationViewController properties
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"detailview"]) {
// Set your destinationViewController properties here
}
}

Related

How to fix UISearchBar returning NSRangeException when clear button is clicked? (FIXED!!!)

I have implemented a UISearchBar in my app that is crashing when I hit the clear button and while there's one item found. It's like I type in some character, the app searches and display 1 item, but I when I clear the search, it crashes.
I searched stackoverflow and I am aware that the problem is with the array that holds the search items cause when I hit the clear button the array becomes empty and the tableview index path.row can't be displayed.
Also, if the search returns no item and I hit the clear button I get a similar error message buy this time saying the I got an empty array.
The app stops here:
NSDictionary *itemAtIndice =(NSDictionary *)[filteredCodigos objectAtIndex:indexPath.row];
The problem is that I simple don't know what to do!!!
Here is the error:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
And here is my code::
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if (searchText.length == 0) {
isFiltered = NO;
} else {
isFiltered = YES;
filteredCodigos = [[NSMutableArray alloc]init];
for (NSDictionary *item in values) {
NSString *string = [item objectForKey:#"NAME"];
NSRange stringRange = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (stringRange.location != NSNotFound) {
[filteredCodigos addObject:item];
}
}
}
[self.tableView reloadData];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[self.Pesquisa resignFirstResponder];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (isFiltered) {
return [filteredCodigos count];
}
return values.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSDictionary *itemAtIndex =(NSDictionary *)[values objectAtIndex:indexPath.row];
NSDictionary *itemAtIndice =(NSDictionary *)[filteredCodigos objectAtIndex:indexPath.row];
if (!isFiltered) {
cell.textLabel.text = [itemAtIndex objectForKey:#"SYMBOL"];
cell.detailTextLabel.font = [UIFont systemFontOfSize:10];
cell.detailTextLabel.text = [itemAtIndex objectForKey:#"NAME"];
} else {
cell.textLabel.text = [itemAtIndice objectForKey:#"SYMBOL"];
cell.detailTextLabel.font = [UIFont systemFontOfSize:10];
cell.detailTextLabel.text = [itemAtIndice objectForKey:#"NAME"];
}
return cell;
}
filteredCodigos is declared outside of searchBar:textDidChange: and is used in multiple places but is assigned to in searchBar:textDidChange:. Most likely, some other code is trying to access members of filteredCodigos which is unassigned or empty. Enabling exception breakpoints will help you track it down.
As a side note. You might look at NSPredicate to filter your items instead of the for loop.
Finally managed to fix this issue.
I just replaced indexPath.row for lastObject at the line that was giving me the error:
So, before:
NSDictionary *itemAtIndice =(NSDictionary *)[filteredCodigos objectAtIndex:indexPath.row];
And after:
NSDictionary *itemAtIndice =(NSDictionary *)[filteredCodigos lastObject];

How to customise UITableView

Actually I'm trying to implement different images in different rows in UITableView.
by creating a new UITableViewCell I tried a lot to implement this but i failed.I need to set two images and a label in each row.
How can it be done?
And i have to get the image in next page using navigation controller...
please specify me the codes and explain.I couldn't understand it otherwise.
Thanks for your kind informative answers...
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
sampletable1 *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"sampletable1" owner:nil options:nil];
for (UIView *view in views) {
if([view isKindOfClass:[UITableViewCell class]])
{
cell = (sampletable1*)view;
}
}
}
Here is what you need.Subclassing UITableViewCell. The sample shown has two images in there. You can use the same technique to add one more

Button action with Segue and Alertview

I have UIStoryboardSegue setup and want to perform an AlertView if a certain condition exists, cancel the alert to stop performSegue and OK to carry on with performSegue. Only problem I have is dealing with the sender id, since the performSeguewithIdentifier needs to go in the alertView button click method. How do I keep the sender(id)?
- (IBAction)drink1Button:(id)sender {
if([drink1Units.text intValue] > 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"You Chose" message:#"" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
[alert show];
[alert release];
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"DrinkSegue"]) {
// Get destination view
DrinksListViewController *selectedDrink = [segue destinationViewController];
// Get button tag
NSInteger tagIndex = [(UIButton *)sender tag];
// Set the selected button in the new view
[selectedDrink setSelectedButton:tagIndex];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
[self performSegueWithIdentifier:#"DrinkSegue" sender:sender];
}

Thread 1:EXC_BAD_ACCESS(code=2, address=0xbf7ffffc)

Am writing a simple multi view application with one root controller and two view controllers (blue and yellow). When I try to run it in the iPhone Simulator, I get an error by the #synthesize property. I have commented out the error on that line.
Can you tell me what the error means, and how can I get the app to run?
Thank you.
#import "SwitchViewController.h"
#import "BlueViewController.h"
#import "YellowViewController.h"
#interface SwitchViewController ()
#end
#implementation SwitchViewController
#synthesize yellowViewController;
#synthesize blueViewController; //Thread 1:EXC_BAD_ACCESS(code=2, address=0xbf7ffffc)
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)loadView
{
// If you create your views manually, you MUST override this method and use it to create your views.
// If you use Interface Builder to create your views, then you must NOT override this method.
}
- (void)viewDidLoad
{
self.blueViewController = [[BlueViewController alloc]initWithNibName:#"BlueView" bundle:nil];
[self.view insertSubview:self.blueViewController.view atIndex:0];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)switchViews:(id)sender
{
if(self.yellowViewController.view.superview==nil) {
if(self.yellowViewController==nil) {
self.yellowViewController =
[[YellowViewController alloc] initWithNibName:#"YellowView" bundle:nil];
}
[blueViewController.view removeFromSuperview];
[self.view insertSubview:self.yellowViewController.view atIndex:0];
} else {
if (self.blueViewController == nil) {
self.blueViewController =
[[BlueViewController alloc] initWithNibName:#"BlueView" bundle:nil];
}
}
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use
if (self.blueViewController.view.superview == nil) {
self.blueViewController = nil;
} else {
self.yellowViewController = nil;
}
}
#end
Comment out the loadView method in your SwitchViewController, BlueViewController, and YellowViewController. The empty application template was changed to leave these uncommented in recent versions of XCode, but the Beginning iOS Development book you are following used an older version with the stubbed methods pre-commented.

cannot display value passed by MainWindow on NSWindow?

Here's my problem:
I have a MainWindow.xib, Window1.xib, Window1Controller.h, Window1Controller.m
I can display Window1 from MainWindow, but the passing value does not display on Window1, I know that value is ready to display, I create a button on Window1 and I can invoke this value. How can I display passing value after loaded Window1.
Here's my code:
AppDelegate.m
- (IBAction)openWindow1:(id)sender {
Window1Controller *w1 = [[Window1Controller alloc]initWithWindowNibName:#"Window1"];
[w1 showWindow:nil];
}
Window1Controller.m
- (void)displayInfo {
[label setStringValue:#"sample passed text"];
NSLog(#"%#",[label stringValue]);
}
- (id)initWithWindow:(NSWindow *)window
{
self = [super initWithWindow:window];
if (self) {
// Initialization code here.
}
return self;
}
- (void)windowDidLoad
{
[super windowDidLoad];
// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
[self displayInfo];
}
Did you connect the outlet of the label to your window controller? (and is your window controller the files owner of your window1.xib?)

Resources