Detail Text for Tableview in popover in Iphone SDK - iphone

I open a PopOverview which contains a table View. It works fine But my cell also contain the detail Text which is not seen in table view cell when i open table view in popOver.
My code is as follow:
-(IBAction)btnTableMenu_TouchUpInside:(id)sender{
ListView *popUp=[[ListView alloc] initWithNibName:#"ListView" bundle:nil];
popoverController = [[UIPopoverController alloc]initWithContentViewController:popUp];
popoverController.delegate =self;
[popoverController setPopoverContentSize:CGSizeMake(300, 700)];
[popoverController presentPopoverFromRect:CGRectMake(150,25,20,50) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
How can i solve This??

Try this one :
Add the observer in the class where your popOverController view is
your popOverController class
yourPopOverControlle.m
-(void)viewdidLoad{
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(removePopover:) name:#"hidePopOver" object:nil];
}
-(void)removePopover:(NSNotification *)notification{
[yourPopOver dismissPopoverAnimated:YES];
}
yourTableViewController.m
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[[NSNotificationCenter defaultCenter]postNotificationName:#"hidePopOver" object:nil];
}

To see detailed text label you need create you cell with UITableViewCellStyleSubtitle style;
For example :
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell = nil;
cell = [_tableView dequeueReusableCellWithIdentifier:kTableCellIdentifier];
if(cell==nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kTableCellIdentifier] autorelease];
}
// configure cell
cell.textLabel.text = "text label text";
cell.detailTextLabel.text = "detailed text label text";
return cell;
}

see user167.. you just need to create new UITableViewcontroller for example DropDwnLevel1TableViewController.h, DropDwnLevel1TableViewController.m and xib right.
Now in DropDwnLevel1TableViewController Create IBOutlate of UITableVIew set one UITableView in to nib connect IBOUTLATE and set Delegate and dataSource
Now you can set created TableViewController add in to your UIPopoverViewcontroller like below method:-
-(IBAction)btnTableMenu_TouchUpInside:(id)sender{
DropDwnLevel1TableViewController *firstViewCtrl = [[DropDwnLevel1TableViewController alloc] init];
firstViewCtrl.title=#"My tableView";
UINavigationController *navbar = [[UINavigationController alloc] initWithRootViewController:firstViewCtrl];
[firstViewCtrl contentSizeForViewInPopover];
myPopOVer = [[UIPopoverController alloc] initWithContentViewController:navbar];
[navbar release];
myPopOVer.delegate = self;
myPopOVer.popoverContentSize =CGSizeMake(250,200);
[myPopOVer presentPopoverFromRect:sender.frame inView:sender.superview permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
}
FOR dismiss popOVer
In you class which declare popOverViewcontroller .m file ViewDidLoad method
- (void)viewDidLoad
{
// Hear creating NSNotificationCenter for dismiss popover
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(DismissPopOver:)
name:#"Dismiss"
object:nil];
[super viewDidLoad];
}
-(void)DismissPopOver:(NSNotification *)notification {
[yourPopOVer dismissPopoverAnimated:YES];
}
Now in DropDwnLevel1TableViewController.m didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// hear call NSNotificationCenter who creating in main class
[[NSNotificationCenter defaultCenter] postNotificationName:#"Dismiss" object:self];
}

Related

resignFirstResponder is not working as expected

I am having the uitextfield to input country value inside the uitableview. When tapping on that field I am showing the tableview with country values loaded.
When tapping on any cell, it comes back to the same form and adds the selected value inside the country field. I mean I assign the selected value to the textfield inside the viewwillappear method. After setting the text inside the country field. I want to resign the keyboard, so I added resignFirstResponder, but it is not resigning the keyboard.
Below is my code:
- (void)textFieldDidBeginEditing:(UITextField *)textField;
{
if( textField == self.myCountryField ){
aCountryListView =
[[LCCountryListViewController alloc]init];
UINavigationController *navigationController =
[[UINavigationController alloc]
initWithRootViewController:aCountryListView];
[self presentModalViewController:navigationController animated:YES];
}
}
-(void)viewWillAppear:(BOOL)animated{
//-----------------------------------
self.myCountryField.text = aCountryListView.mySelectedCountry;
[self.myCountryField resignFirstResponder];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
//-------------------------------------------------------------------------------
{
static NSString *CellIdentifier = #"PoetNameCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor = [UIColor clearColor];
if( indexPath.row == 0 ){
UILabel *aLblTitle = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 220, 30)];
aLblTitle.backgroundColor = [UIColor clearColor];
aLblTitle.font = [UIFont fontWithName:#"Palatino-Bold" size:16.0];
aLblTitle.text = NSLocalizedString(#"Country","Country");
aLblTitle.textColor = [UIColor whiteColor];
[cell.contentView addSubview:aLblTitle];
myCountryField = [[UITextField alloc]initWithFrame:CGRectMake(133,5,150,30)];
myCountryField.backgroundColor = [UIColor whiteColor];
myCountryField.delegate = self;
myCountryField.borderStyle = UITextBorderStyleRoundedRect;
[cell.contentView addSubview:myCountryField];
}
return cell;
}
use the below code, hope it will help you out.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self.view endEditing:YES];
return YES;
}
ensure that you will connect your delegate to fileowner in xib-file. or in viewDidload use the following code.
yourtextfield.delegate=self;
and in your .h file use <UITextfielddelegate>
Hope the following code will work when add UITextField delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self.myCountryField resignFirstResponder];
return YES;
}
How about using this instead:
[[UIApplication sharedApplication] sendAction:#selector(resignFirstResponder)
to:nil
from:nil
forEvent:nil];

Application tried to present a nil modal view controller error

I've been trying to push a uiwebview from a tableview to no avail. This is the code I have right now and whenever I click the cell, it would crash and show me the error.
Courses.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([[array objectAtIndex:indexPath.row] isEqual:#"GolfClub"])
{
Blue *changi = [[Blue alloc] initWithNibName:#"Golf Club" bundle:nil];
[self.navigationController pushViewController:changi animated:YES];
[changi release];
}
else if ([[array objectAtIndex:indexPath.row] isEqual:#"ExecutiveGolfCourse"])
{
ExecutiveGolfCourse *egc = [[ExecutiveGolfCourse alloc] initWithNibName:#"Executive Golf Course" bundle:nil];
[self.navigationController pushViewController:egc animated:YES];
[egc release];
}
newsClicked.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:newsClicked animated:YES];
}
initWithNibName is not meant for the title of your ViewController
nib name is actually the name of the .xib file that is associated with that ViewController

How to link a UITableViewController to multiple ViewControllers and how to call them

I'm trying to link cells from an UITableViewController to UIViewController, but each cell has to call a different UIViewController (so I imagine that I'll have to create one link to each view controller) and I don't know how to link them through storyboard.
This is what I have until now:
MainView.h
#interface MainView : UITableViewController <UITableViewDelegate, UITableViewDataSource>
{
NSArray *tableData;
}
#property (nonatomic, retain) NSArray *tableData;
#end
MainView.m
- (void)viewDidLoad
{
tableData = [[NSArray alloc] initWithObjects:#"1", #"2", #"3", #"4", nil];
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"MyCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MyCell"];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UIViewController *anotherVC = nil;
if (indexPath.section == 0) //here I've also tried to use switch, case 0, etc
{
if (indexPath.row == 0)
{
anotherVC = [[ViewForFirstRow alloc] init];
}
NSLog(#"anotherVC %#", anotherVC); // shows that anotherVC = ViewForFirstRow
[anotherVC setModalPresentationStyle:UIModalPresentationFormSheet];
[anotherVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self.navigationController pushViewController:anotherVC animated:YES];
}
}
#end
So, when first row is selected and I didn't link UITableViewController to UIViewController (ViewForFirstRow), it shows a black screen.
If I link UITableViewController to ViewForFirstRow, it opens the view but without the effect I used in code [self.navigationController pushViewController:anotherVC animated:YES];, so it appears that it called the view due to the link and not because of the code [self.navigationController pushViewController:anotherVC animated:YES];
How can I call the view by code properly and what kind of link should I use in storyboard for that? Also, how can I link the UITableViewController to multiples UIViewController (ViewForFirstRow, ViewForSecondRow, ViewForThirdRow, etc).
Thanks!
First, you can't hook up segues from cell (in a dynamic table view) in the storyboard, you have to do it in code. The code should look like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.row == 0) {
UIViewController *anotherVC = [self.storyboard instantiateViewControllerWithIdentifier:#"anotherVC"];
[self.navigationController pushViewController:anotherVC animated:YES];
}else if (indexPath.row == 1) {
UIViewController *someOtherVC = [self.storyboard instantiateViewControllerWithIdentifier:#"someOtherVC"];
[self.navigationController pushViewController:someOtherVC animated:YES];
}
}
You shouldn't be doing anything with indexPath.section since your table view only has one section.
In storyboard presenting another viewcontroller in modally manner then use this:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
Synergy *objSynergy = (Synergy *)[mainStoryboard instantiateViewControllerWithIdentifier:#"Synergy"];
[self presentModalViewController:objSynergy animated:YES];
Synergy is my controller name You can put here your controller name that you want to present.
Make changes with your requirement. And for different viewcontroller open from different row
Use this:
if (indexPath.section==0)
{
if (indexPath.row ==0)
{
}
}

UITableView Crashes when scrolling

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(#"Returning num sections");
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"Returning num rows");
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Trying to return cell");
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] ;
}
cell.textLabel.text = #"Hello";
NSLog(#"Returning cell");
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Selected row");
}
- (void)viewDidLoad
{
[super viewDidLoad];
m_titleTable = [[UITableView alloc] init] ;
m_titleTable.dataSource = self;
m_titleTable.delegate = self;
m_titleTable.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:m_titleTable];
// Do any additional setup after loading the view from its nib.
}
-(void)titleAction:(id)sender{
NSLog(#"Calling");
UIViewController* popoverContent = [[UIViewController alloc]
init];
TitleViewController *popoverView = [[TitleViewController alloc]initWithNibName:#"TitleViewController" bundle:nil];
popoverContent.view = popoverView.view;
//resize the popover view shown
//in the current view to the view's size
popoverContent.contentSizeForViewInPopover = CGSizeMake(150, 150);
//create a popover controller
self->popoverController = [[UIPopoverController alloc]
initWithContentViewController:popoverContent];
//present the popover view non-modal with a
//refrence to the button pressed within the current view
[self->popoverController presentPopoverFromRect:btn_title.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
}
The tableview Crash error message
[__NSCFType tableView:cellForRowAtIndexPath:]: unrecognized selector sent to instance 0x6e7dca0
2012-08-28 14:17:10.539 Demo[2790:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType tableView:cellForRowAtIndexPath:]: unrecognized selector sent to instance 0x6e7dca0'
Your problem is that your view controller is being deallocated. You shouldn't be doing something like this:
UIViewController* popoverContent = [[UIViewController alloc]
init];
TitleViewController *popoverView = [[TitleViewController alloc]initWithNibName:#"TitleViewController" bundle:nil];
popoverContent.view = popoverView.view;
Assuming you are using ARC, your popoverView object will be deallocated. Also this is just incorrect. You should almost never instantiate a UIViewController, and definitely never assign one view controller's view instance to another ones!
Here's how I'd rewrite your titleAction: method:
TitleViewController *titleViewController = [[TitleViewController alloc]initWithNibName:#"TitleViewController" bundle:nil];
//resize the popover view shown
//in the current view to the view's size
popoverContent.contentSizeForViewInPopover = CGSizeMake(150, 150);
//create a popover controller
self.popoverController = [[UIPopoverController alloc]
initWithContentViewController:titleViewController];
//present the popover view non-modal with a
//refrence to the button pressed within the current view
[self.popoverController presentPopoverFromRect:btn_title.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
The UIPopoverController, at least, should keep a strong reference to the titleViewController and prevent it from being deallocated.
PS I changed your "self->popoverController" to self.popoverController as the -> is not really correct for what you are trying to do. Dot notation makes explicit that you are setting a property on an object.
in viewDidLoad you allocates and initiates a UITableView and connect it to self. You don't have a UITableView? defined and connect in the nib? Without seeing your whole project my guess is that you have UITableViews - this is a problem but not necessarily the crash problem. Did you inherit from UITableViewController as base class?

how display view when cells select in a tableview

i am new to iphone ,my question is in one view i placed a tableview and display some data on that view,i want to add another view(same view to all cell) when select cell(suppose select first cell view will display and newly add view is used to delet that cell information based on cell index)please tell me how to display view.
In the didSelectRowAtIndexPath method in your TableView class; that is where you deal with the selected row. Here is some code that pushes a webViewController onto the screen :
webViewController = [[WebViewController alloc] init];
WebViewController *wvc = [[WebViewController alloc] init];
NSMutableArray *temp = [eventsDict objectForKey:[datesArray objectAtIndex:[indexPath section]]];
EventObject *tempEvent = [temp objectAtIndex:[indexPath row]];
NSString *eventURL = [tempEvent eventLink];
[wvc setEventWebsite:eventURL];
[self setWebViewController:wvc];
[[self navigationController] pushViewController:webViewController animated:YES];
[wvc release];
[webViewController release];
Basically I have an array of Events, it gets the event you touched using [indexPath row], gets the URL within that Event object, passes it to the webViewController, then it pushes the webViewController onto the screen loading the appropriate link. Hope this helps
If you want to display the same view for all cells - simply create one UIView object and change frame in didSelectRowAtIndexPath delegate method. It will something like this -
At first declare infoView in your .h file UIView *infoView, next steps:
- (void)viewDidLoad {
infoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
infoView.hidden = YES;
[tableView addSubview:infoView];
[super viewDidLoad]; }
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
CGPoint st = CGPointMake(tableView.frame.size.width/2, cell.frame.size.height*indexPath.row);
infoView.hidden = NO;
infoView.center = st;}
Don't forget to release infoView in dealloc method