subView in DetailViewController? - iphone

I have a main table view and a detail view. when i click cell detail view should show the details of that item. now what i wanna do is to create subview and put that subview in detailView instead of creating a detailview for every cell. it ll solve my some other problems. Code looks like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *nextController = [[DetailViewController alloc] init];
int storyIndex = [indexPath indexAtPosition:[indexPath length] -1];
[nextController initWithObjectAtIndex:storyIndex inArray:stories];
NSString *storyTitle = [[stories objectAtIndex:storyIndex] objectForKey:#"title"];
nextController.title = #"Details";
UIBarButtonItem *tempButtonItem = [[[UIBarButtonItem alloc] init] autorelease];
tempButtonItem.title = #"Tillbaka";
self.navigationItem.backBarButtonItem = tempButtonItem ;
[self.navigationController pushViewController:nextController animated:YES];
[nextController release];
}
now how can i create a subview with a label and change text of that label to storyTitle.
thanx in advance.

how can i create a subview with a label and change text of that label to storyTitle
Given a parent view, you don't need to create a new subview just to add a label to the parent. But, assuming you really do want to add a subview with a label to your parent view, then do this:
...
UIView *parentView;
...
UIView *subView = [[[UIView alloc] initWithFrame:mySubViewFrame] autorelease];
UILabel *lbl = [[[UILabel alloc] initWithFrame:myLabelFrame] autorelease];
lbl.text = storyTitle;
[subView addSubview: lbl];
[parentView addSubview: subView];

Related

Hide UIPickerview On Done Button in UITableView

I have UITextField in each Cell of UITableview and I have added UIPickerview as inputView of UITextField and showing with Done button at its tool bar
My question is how can I hide this this pop up (Picker + toolbar) on click of done button ?
and show selected value of picker in text box in particular cell ?
Thanks and Regards
Edit : Code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
PremiumProductsDescriptionCell *cell = (PremiumProductsDescriptionCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[PremiumProductsDescriptionCell alloc] initShoppingCartCellWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
ShopProduct *p = (ShopProduct *)[[ShopProduct GetShoppingCart] objectAtIndex:indexPath.row];
cell.Quantity.text = [NSString stringWithFormat:#"%d",p.Quantity];
UIPickerView *quantityPicker = [[UIPickerView alloc] init];
quantityPicker.dataSource = self;
quantityPicker.delegate = self;
UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:
CGRectMake(0,0, 320, 44)];
UIBarButtonItem *doneButton =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self action:#selector(hideKeyBoard)];
quantityPicker.tag = indexPath.row;
[myToolbar setItems:[NSArray arrayWithObject: doneButton] animated:NO];
cell.Quantity.inputAccessoryView = myToolbar;
cell.Quantity.inputView = quantityPicker;
cell.Quantity.delegate = self;
return cell;
}
Solved :
I have taken currentTextBox a variable and added following method and resizing its first responder in done button's click :)
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
currentTextBox = textField;
}
UIPopOver cant be dismissed from their class and you need to dismiss it from the calling class.
You have to call dismiss method from popover calling class, when user presses the done button
-(void)doneButtonClikd
{ ParentClass *viewController=[ParentClass alloc]init];
[viewController dismissPopOver];
}
I think this will solve your problem
For your inputview-
-(void)doneButtonclikd
{ [selectedTextfield resignFirstResponder];
}
Dont forget to save the currently selected textfield.
Assuming you put the UIPickerView in a popover, here's how to do it:
UIPopoverController* popover = ....
UIBarButtonItem* doneButton = ....
[doneButton addTarget:self action:#selector(closeMe)
forControlEvents:UIControlEventTouchUpInside]
// ....
- (void)closeMe
{
// Assuming popover is really a field or something...
[popover dismissPopoverAnimated:YES];
}
Use [self.view endEditing:YES] method.

add uiview at row selection

I want when tab on row a UIView containing two button appear at the center of the cell, so I did the following code
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//how can I get the text of the cell here?
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIView *v = [[UIView alloc] init];
v.center = cell.center;
UIButton*button1 =[[UIButton alloc] init];
button1.frame = CGRectMake(v.center.x - 5 , v.center.y , 5 , v.center.y + 4 );
button1.titleLabel.text = #"I'm label 1";
[v addSubview: button1];
UIButton*button2 =[[UIButton alloc] init];
button2.frame = CGRectMake(v.center.x - + 1 , v.center.y , 5 , v.center.y + 4 );
button2.titleLabel.text = #"I'm label 2";
[v addSubview: button2];
[cell addSubview:v];
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
}
UIView doesn't appear. How to solve that?
try:
[cell.contentView addSubview: v];
It should work.
Edit:
Try this code. Give the frames accordingly, it will work.
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(44, 100, 200, 200)];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0, 0, 50, 50);
[v addSubview:button];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell.contentView addSubview:v];
UIView *v = [[UIView alloc] init];//You have not set the frame of the v;
initWithFrame is the designated initialiser for views.
Since you are using init it doesn't know how large a view to create.
Also UIButton *button1 = [[UIButton alloc] init]; is incorrect. UIButtons are created with the buttonWithType: class method.

adding a uinavigationcontroller to a view as part of a stack of uiviews

In my iphone app, I first call a controller which stack up a few view controllers based on index. This is done cause based on the user's selection, i will need to show different screens (basically, i have a welcome screen, tabbar view - which is the main app, sign in and sign up pages). As of now, everything works perfectly - I stack them up and remove / switch based on the need. The problem is that i would like to add a nav bar to both the sign in and sign up views. However, when i do that, something weird happens - i see a nav bar, but on TOP of it, there is a white bar as well (half width more or less). How can i add this uinavbar successfully?
Here is the controller that is being called by the AppDelegate:
- (void)viewDidLoad
{
[super viewDidLoad];
myTabBarVC = [[tabBarController alloc] initWithNibName:#"tabBarController" bundle:nil];
[self.view insertSubview:myTabBarVC.view atIndex:0];
myLoginVC = [[loginViewController alloc] initWithNibName:#"loginViewController" bundle:nil];
[self.view insertSubview:myLoginVC.view atIndex:1];
mySignUp = [[SignUpView alloc] initWithNibName:#"SignUpView" bundle:nil];
[self.view insertSubview:mySignUp.view atIndex:2];
myWelcomeView = [[WelcomeView alloc] initWithNibName:#"WelcomeView" bundle:nil];
[self.view insertSubview:myWelcomeView.view atIndex:3];
}
When I add that to one of the view controllers in this same method, it doesnt work, as described above.
myLoginVC = [[loginViewController alloc] initWithNibName:#"loginViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myLoginVC];
[self.view insertSubview:navController.view atIndex:1];
How can I make it work? Please help. Thanks!
ADDING MORE INFO ABOUT THE VIEW WHERE THE KEYBOARD IS ON BY DEFAULT:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if( cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
cell.textLabel.text = [[NSArray arrayWithObjects:#"",#"",nil]
objectAtIndex:indexPath.row];
if (indexPath.row == 0) {
textField1 = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 280, 21)];
textField1.delegate = self;
textField1.placeholder = #"example#EMAIL.com";
textField1.text = [inputTexts objectAtIndex:indexPath.row/2];
textField1.tag = indexPath.row/2;
cell.accessoryView = textField1;
textField1.returnKeyType = UIReturnKeyNext;
textField1.clearButtonMode = UITextFieldViewModeWhileEditing ;
[textField1 becomeFirstResponder];
textField1.tag = 1;
}
else
{
textField2 = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 280, 21)];
textField2.delegate = self;
textField2.placeholder = #"password";
textField2.text = [inputTexts objectAtIndex:indexPath.row/2];
textField2.tag = indexPath.row/2;
textField2.returnKeyType = UIReturnKeyDone;
textField2.clearButtonMode = UITextFieldViewModeWhileEditing;
cell.accessoryView = textField2;
[textField1 becomeFirstResponder];
textField1.tag = 2;
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
I don't think you should be adding navController.view as a subview to your view controller. I suspect that's a source of the trouble. Instead use a UINavigationBar object.
With regards to your second question, if it was a question and not a remark, about the keyboard being by default, that happens because you set your textField1 as first responder.

Highlighted TableView problem

I'm working with a customized TableView and I have a problem when I click on a cell and it gets highlighted. After clicking on the cell, it gets highlighted and takes me to another view. The problem is that, when I return to the TableView screen, the cell is still highlighted.
Here is my code:
In cellForRowAtIndexPath
cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage
imageNamed:#"CustomCell.png"]] autorelease];
cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"CellHighlighted.jpg"]] autorelease];
In didSelectRowAtIndexPath
Characteres *vCharacters = [[Characters alloc] initWithNibName:#"Characters" bundle:[NSBundle mainBundle]];
//vCharacters.selectedCountry = selectedCountry;
[self presentModalViewController:vCharactersr animated:YES];
[vCharacters release];
vCharacters = nil;*
You need to add this to didSelectRowAtIndexPath:
[tableView deselectRowAtIndexPath:indexPath animated:NO];
This will deselect the row.

can i display alert view with the table view with segmented controls in every cell

i want to display the table view in alert view. This table view contains segmented control.
these segmented controls are for on/off the audio, image, text. so there will be 3 cells with segmented controls.
how to do this
please help me out
Thank u
Adding UISegmentedControls (or UISwitches, which I would personally recommend for a simple on/off option) is easy enough to put in table view cells using the accessoryView property.
Your view controller (or whatever object you're using to control this thing) must implement the UITableViewDataSource protocol.
#interface MyViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> {}
#end
Add the controls in tableView:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString* const SwitchCellID = #"SwitchCell";
UITableViewCell* aCell = [tableView dequeueReusableCellWithIdentifier:SwitchCellID];
if( aCell == nil ) {
aCell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SwitchCellID] autorelease];
aCell.textLabel.text = [NSString stringWithFormat:#"Option %d", [indexPath row] + 1];
aCell.selectionStyle = UITableViewCellSelectionStyleNone;
UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
aCell.accessoryView = switchView;
[switchView setOn:YES animated:NO];
[switchView addTarget:self action:#selector(soundSwitched:) forControlEvents:UIControlEventValueChanged];
[switchView release];
}
return aCell;
}
Or, if you're set on segmented controls, replace the UISwitch stuff above with something like:
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"On", #"Off", nil]];
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.frame = CGRectMake(75, 5, 130, 30);
segmentedControl.selectedSegmentIndex = 0;
[segmentedControl addTarget:self action:#selector(controlSwitched:) forControlEvents:UIControlEventValueChanged];
aCell.accessoryView = segmentedControl;
[segmentedControl release];
I would personally put these options in a regular view (probably accessible from an options button which does the flip transition, like many iPhone apps do). I don't think putting it in an alert view is a very good user experience.
However, I have written a few blog posts showing how to put various views into alert views (text field, which is kinda useful, and web view, which is arguably not).
So, if you're really dead set on putting this in an alert view, you can totally do that, like this:
- (void) doAlertWithListView {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Preferences"
message:#"\n\n\n\n\n\n\n"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
UITableView *myView = [[[UITableView alloc] initWithFrame:CGRectMake(10, 40, 264, 150)
style:UITableViewStyleGrouped] autorelease];
myView.delegate = self;
myView.dataSource = self;
myView.backgroundColor = [UIColor clearColor];
[alert addSubview:myView];
[alert show];
[alert release];
}
It will look something like this:
The answer above works great, but on the iPad the line myView.backgroundColor = [UIColor clearColor];
doesn't seem to remove the grey rectangular area around the table. I find that the following does the job: tableView.backgroundView.alpha = 0.0;