So fügen Sie einer Navigationsleiste im iPhone eine rechte Balkentaste hinzu
-
27-10-2019 - |
Frage
Ich möchte der Navigationsleiste ein Element der rechten Balken -Schaltfläche hinzufügen, damit es bei Klick eine bestimmte Funktion erbringt.
Ich habe den folgenden Code erstellt, um das richtige Element der richtigen Balken -Schaltfläche hinzuzufügen. Nachdem er fertig ist, wird das Element der Balkenschaltfläche nicht in der Navigationsleiste angezeigt:
-(void)viewDidload{
self.navigationItem.rightBarButtonItem =
[[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(Add:)] autorelease];
}
-(IBAction)Add:(id)sender
{
TAddNewJourney *j=[[TAddNewJourney alloc]init];
[app.navigationController pushViewController:j animated:YES];
[j release];
}
Lösung
-(void)viewDidLoad
{
[super viewDidLoad];
app.navigationController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(Add:)] autorelease];
}
-(IBAction)Add:(id)sender
{
TAddNewJourney *j=[[TAddNewJourney alloc]init];
[app.navigationController pushViewController:j animated:YES];
[j release];
}
Probieren Sie die anderen Antworten aus. Ich habe diese Antwort so gepostet, dass sie funktioniert, wenn Ihr ViewController keinen Navigationscontroller hat, von dem ich denke, dass es das Problem ist.
Andere Tipps
Nehmen wir an, Sie haben einen UIViewController wie folgt:
UIViewController *vc = [UIViewController new];
Und Sie haben es dem Navigationscontroller hinzugefügt:
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
Auf diese Weise wird es rechtzeitig Nicht angezeigt werden:
nc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"xyz" style:UIBarButtonItemStyleDone target:self action:@selector(xyz)];
Aber so wird es erscheinen:
vc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"xyz" style:UIBarButtonItemStyleDone target:self action:@selector(xyz)];
Verwenden Sie Ihren eigenen ViewController anstelle des NavigationController, um NavigationItem zu verweisen.
Fügen Sie diesen Code in ViewDIDLOAD hinzu
UIBarButtonItem *chkmanuaaly = [[UIBarButtonItem alloc]initWithTitle:@"Calculate" style:UIBarButtonItemStylePlain target:self action:@selector(nextview)];
self.navigationItem.rightBarButtonItem=chkmanuaaly;
[chkmanuaaly release];
Die meisten Antworten hier befassen sich mit dem RightbartonItem der UinavigationBar -Rightbartonitem aus dem neuen VC, nachdem er gezeigt wurde. Mein Bedürfnis, das tatsächlich von János beantwortet wurde, ist es, die uinAvigationItem -Elemente aus dem Anruf UiviewController festzulegen. Daher der folgende Code (danke, jános).
// von innen in sich selbst (auch bekannt als der aufrufende Controller):
UIViewController *c = [[[UIViewController alloc] init...] autorelease];
c.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissModalViewControllerAnimated:)] autorelease];
c.navigationItem.title = @"Title Goes Here";
UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:c] autorelease];
nav.navigationBarHidden = FALSE;
[self presentModalViewController:nav animated:YES];
Zugegeben, dies mag János 'Antwort überflüssig erscheinen, aber ich brauche mehr Wiederholungspunkte, um seine Antwort zu markieren. ;-)
Ich denke, es wird eine vollständigere Antwort sein und sollte in jeder Situation helfen:
-(void)viewDidLoad{
//Set Navigation Bar
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
//Set title if needed
UINavigationItem * navTitle = [[UINavigationItem alloc] init];
navTitle.title = @"Title";
//Here you create info button and customize it
UIButton * tempButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
//Add selector to info button
[tempButton addTarget:self action:@selector(infoButtonClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * infoButton = [[UIBarButtonItem alloc] initWithCustomView:tempButton];
//In this case your button will be on the right side
navTitle.rightBarButtonItem = infoButton;
//Add NavigationBar on main view
navBar.items = @[navTitle];
[self.view addSubview:navBar];
}
UIBarButtonItem *add=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addUser)];
self.navigationItem.rightBarButtonItem=add;
[add release];
Verwenden Sie den folgenden Code:
UIBarButtonItem *add=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addUser)];
self.navigationItem.rightBarButtonItem=add;
[add release];
Hoffe das hilft dir aus.
Genießen!
UIButton *dButton=[UIButton buttonWithType:0];
dButton.frame=CGRectMake(50,50,50,50);
[dButton addTarget:self action:@selector(clickdButton:)
forControlEvents:UIControlEventTouchUpInside];
[dButton setImage:[UIImage imageNamed:@"iconnavbar.png"]
forState:UIControlStateNormal];
dButton.adjustsImageWhenHighlighted=NO;
dButton.adjustsImageWhenDisabled=NO;
dButton.tag=0;
dButton.backgroundColor=[UIColor clearColor];
UIBarButtonItem *RightButton=[[[UIBarButtonItem alloc] initWithCustomView:dButton]autorelease];
self.navigationItem.rightBarButtonItem=RightButton;
und dann:
-(IBAction)clickdButton:(id)sender{
classexample *tempController=[[classexample alloc] init];
[self.navigationController pushViewController:tempController animated:YES];
[tempController autorelease];
}
Schnell:
let rightButton = UIBarButtonItem(image: UIImage(named: "imageName"),
style: .plain,
target: self,
action: #selector(buttonTapped))
navigationItem.setRightBarButton(rightButton, animated: false)
func settingsButtonTapped() {
// Implement action
}