UIButton in UITableView header not working? - xcode

I have added a UIBUtton in a UITableView header and it doesn't seem to be working. It's clickable, but when attaching a IBAction to it, it fails and does nothing.
I have read online and saw to put it in your File's Owner, but I can't do that. When I put the action in, it only shows up in my First Responder.
I have also tried to put it in a different file, but when doing do it won't let me drag it to the other file.
FILES:
RootViewController.h
RootViewController.m
TableViewAppDelegate.h
TableViewAppDelegate.m
DetailViewController.h
DetailViewController.m
RootViewController.h
#interface RootViewController : UITableViewController {
NSMutableArray *listOfItems;
Sqlite *database;
NSTimer *myTimer;
UITextView *myTextField;
}
- (IBAction)addAlbum;
RootViewController.m
#import "RootViewController.h"
#import "TableViewAppDelegate.h"
#import "DetailViewController.h"
#implementation RootViewController
// If a user adds a photo album
- (IBAction)addAlbum {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Add Gallery" message:#"this gets covered" delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:#"Ok", nil];
myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0, -80);
[alert setTransform:myTransform];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:myTextField];
[alert show];
[alert release];
[alert setTag:1];
}
Any help is appreciated!
Thanks.

If you're loading the table header view from .xib, make "File's Owner" of the .xib your ViewController subclass presenting the UITableView, and connect the desired event in the button to the desired IBAction in File's Owner. If you're creating the button programmatically in a tableView:viewForHeaderInSection: method, you must wire up the action yourself on the button using this method on the button object:
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
EDIT: you should post your tableView:viewForHeaderInSection: code so we can give more specific advice.

Related

xcode How to link UIAlertview action to another viewcontroller?

Im am trying to link one of my buttons from the UIAlertView that pops up so that It changes to another viewcontroller. Basically, the cancel already works, but when Next is clicked it doesnt, and i don't know how to link to another viewcontroller. Please help.
#import "ViewController.h"
#interface ViewController() <UIAlertViewDelegate>
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)clickButton:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Test" message: #"Message" delegate: self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Redeem", nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 1){ //Next
UIViewController* newView = [[UIViewController alloc] init];
[self presentViewController:newView animated:YES completion:nil];
}
}
#end
It is working...Just create object of viewcontroller that you want to present instead of
UIViewController* newView = [[UIViewController alloc] init];
like
SecondViewController * newView = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
if you are using xib.
Or
SecondViewController * newView = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
if you are using storyboard(if you want to go through coding).
and then write
[self presentViewController:newView animated:YES completion:nil];
line.

Why is this Cocoa layout ambiguous?

I want to use layout constraints and create my UI programmatically. Here is a simple program that I'm hoping you can help me understand. In Interface Builder, I simply took the defaults -- there is an NSWindow with its default contentView. Below is all the code, and a screenshot.
I create a single button, and place it in the content view. Then I try to use constraints to make it fill the window. As you can see, it claims the layout is ambiguous. But when I click that button to "Exercise Ambiguity", nothing changes. The docs say it should choose a different possible layout.
I also think the content view is tightly surrounding the button and not filling the window, but I don't know how to force that with constraints.
// In AppDelegate.h
#interface AppDelegate : NSObject <NSApplicationDelegate> {
NSButton *_button;
}
#property (assign) IBOutlet NSWindow *window;
#end
// In AppDelegate.m
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSView *contentView = (NSView*)_window.contentView;
_button = [[NSButton alloc] init];
_button.title = #"Test";
_button.translatesAutoresizingMaskIntoConstraints = NO;
contentView.translatesAutoresizingMaskIntoConstraints = NO;
[contentView addSubview:_button];
NSDictionary *viewsDict = NSDictionaryOfVariableBindings(_button, contentView);
NSMutableArray *constraints = [[NSMutableArray alloc] init];
[constraints addObjectsFromArray: [NSLayoutConstraint constraintsWithVisualFormat:#"|[_button]|" options:0 metrics:0 views:viewsDict]];
[constraints addObjectsFromArray: [NSLayoutConstraint constraintsWithVisualFormat:#"V:|[_button]|" options:0 metrics:0 views:viewsDict]];
[contentView addConstraints:constraints];
[_window visualizeConstraints:constraints];
printf("Is layout ambiguous? %d\n", contentView.hasAmbiguousLayout);
}
#end
What if you visualize constraints on a subsequent iteration of the run loop, for example with a timer or by clicking a button, after the layout engine has had a pass at it? It may just be ambiguous because the layout engine hasn’t solved the system yet.
Edit: I ran your code, and am seeing the same issue. I’m also stumped now.

Usernotes in ios

I want to create a user note form in my application,currently am using one textview inside a view its looking bad !! is there any other control suits for this purpose? Main aim is when user click the button a small textview will appear they can add comments there and save it into plist.
I want something like this(check the image)
i want that kind of usernotes (its my image) please give me some advices and helps to develop this..
Using UIAlertView with UITextView can be useful for you.
Implement UIAlertViewDelegate in .h file.
UITextView *comments;
-(IBAction)btnAddClicked:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Noreply Email" message:#"\n\n\n\n\n" delegate:self cancelButtonTitle:#"Close" otherButtonTitles:#"Send", nil];
comments = [[UITextView alloc] initWithFrame:CGRectMake(15,45, 255, 100)];
[comments setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"mailbody.png"]]];
[comments setFont:[UIFont fontWithName:#"Helvetica" size:15]];
comments.scrollEnabled = YES;
[comments becomeFirstResponder];
[alert addSubview:comments];
[alert show];
[alert release];
}
Here alert will be prompted with small textview you can add comments and then handle text inside delegate method like this.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==1) //Send button pressed
{
//handle your comment here
NSLog(#"%#",comments.text);
}
}

IBAction,button and camera

I have a button in my interface declared in .h file
#interface UserProfileVC : UIViewController <UIImagePickerControllerDelegate>{
IBOutlet UIButton *camera;
}
#property (nonatomic,retain) IBOutlet UIButton *camera;
-(IBAction)cameraPress:(id)sender;
And in my .m file i have:
-(IBAction)cameraPress:(id)sender{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
// [picker setDelegate:self];
[picker setAllowsEditing:YES];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
[picker release];
}
But I have this error:
*** -[UserProfileVC performSelector:withObject:withObject:]: message sent to deallocated instance 0x7bc2a40
Can someone help me? I can't understand what is the mistake.
Thanks
As per the code from ur last comment,
-(void)showDetails:(id)sender{
NSLog(#"Annotation Click");
details= [[UserProfileVC alloc] initWithNibName: #"Details" bundle:nil ];
details.Nome=note.title;
addNavigationController = [[UINavigationController alloc] initWithRootViewController:details];
[self.navigationController presentModalViewController:addNavigationController animated:YES];
}
I can suggest you following.
If you look at the UIViewController class reference document, you will find below notes
presentModalViewController:animated:
Presents a modal view managed by the given view controller to the user. (Deprecated. Use presentViewController:animated:completion: instead.)
So I would suggest you to use presentViewController:animated:completion. I dont find it relevant to error "message sent to deallocated instance" but still check if u could solve your problem.
Also I dont know Why u wrote this line
addNavigationController = [[UINavigationController alloc] initWithRootViewController:details];
If you simply want to show UserProfileVC in the current UINavigationController then I would suggest you to remove addNavigationController line & write only
[self.navigationController presentViewController:details animated:YES completion:NULL];

IOS login screen before tab-controller view

I am still newbie for IOS Developing, i want to create a login page by MoralViewcontroller.
AppDelegate.h
#interface AppDelegate : UIResponder <UIApplicationDelegate>{
UITabBarController *tabBarController; }
#property (nonatomic,retain) IBOutlet UITabBarController * tabBarController
AppDelegate.m
(void)applicationDidFinishLaunching:(UIApplication *)application
{
// Override point for customization after app launch
[window addSubview:tabBarController. view];
[window makeKeyAndVisible];
LoginViewController *loginView=[[LoginViewController alloc]initWithNibName:#"LoginViewController" bundle:nil];
[tabBarController.view presentModelViewcontroller: loginView animated:YES];
}
However, the login view cannot be shown, I think I define wrongly for tabBarController, but I don't know what wrong with it. Can anyone please advise me? I am doing IOS 5.
Thanks alot..
I'd present a loginView controller from the rootView of the tabBarController.
-(void)viewDidLoad
{
//You can also do this inside a conditional statement, if needed
LoginViewController *loginView=[[LoginViewController alloc]initWithNibName:#"LoginViewController" bundle:nil];
[self.view presentModelViewcontroller:loginView animated:YES];
}
And here is the second way
AppDelegate.h
#interface AppDelegate : UIResponder {
LoginViewController *loginView;
}
#property (nonatomic,retain) LoginViewController *loginView;
AppDelegate.m
-(void)applicationDidFinishLaunching:(UIApplication *)application
{
// Override point for customization after app launch
self.loginView=[[LoginViewController alloc]initWithNibName:#"LoginViewController" bundle:nil];
[window addSubview:loginView. view];
[window makeKeyAndVisible];
}
LoginViewController.m
Call this method on successful login.
-(IBAction)login:(id)sender
{
//init tabbar with subviews;
UITabBarController *tabBarController = [[UITabBarController alloc] initW....];
[self.view addSubview:tabBarController.view];
}
I prefer first method, because in that you will be retaining the tabBarController in AppDelegate.
First of all you have to add the Loginviewcontroller to the window.And then you have to add the tabbarcontroller to the LoginViewController when the login button clicked.

Resources